Microsoft KB Archive/313812

= PRB: SaveToFile Method Does Not Save Embedded Document in Proper Format =

Article ID: 313812

Article Last Modified on 4/24/2003

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q313812



SYMPTOMS
There may be situations where you want to access an Excel spreadsheets or Word documents, but you may not have the access to Excel or Word environment. This can be achieved by inserting the document in an OLE container. After editing the document, you may save the document programmatically using SaveToFile method of OLE container. When you try to open the document again using Excel or Word application environment, the document will not open in proper format.



CAUSE
The embedded documents are saved in a format other than Excel Spreadsheet or Word document.



WORKAROUND
To workaround this behavior, use the methods of the contained server application instead of the method of the OLE container. The Object property provides access to the contained object and its object model. For example, in order to save a Word Document contained in OLE container in appropriate format, use SaveAs method of Word Application.

OLE1.Object.SaveAS &quot;C:\MyFile.doc&quot;



STATUS
This behavior is by design.



MORE INFORMATION
Steps to Reproduce the behavior  Open Visual Basic 6.0 IDE. On File menu, click New Project. On the New Project dialog box, select Standard Exe and then click OK. By default Form1 is displayed. From the Toolbox drag OLE control and drop it onto the Form1. In the Insert Object dialog box, select Microsoft Word Document Object Type and then click OK. Type some text in the Word document contained in OLE container.</li> From the Toolbox drag a Button control and drop it onto the Form1. In the property window change the Name to cmdSave and caption to Save.</li>  Add the following code to cmdSave_Click event: Dim FileNum As Integer ' Get file number. FileNum = FreeFile ' Open file to be saved. Open &quot;C:\MyFile.doc&quot; For Binary As #FileNum ' Save the changes the file. OLE1.SaveToFile FileNum ' Close the file. Close #FileNum </li> On Run menu, click Start to run the application.</li> Click Save .</li> Open the C:\MyFile.doc in Word Application. The Word Document will not open in proper format.</li>  Replace the code in cmdSave_Click event with the following code: ' Save the file using contained application method OLE1.object.SaveAs (&quot;C:\MyNewFile.doc&quot;) </li> Repeat step 8, 9and 10. This time the Word document opens in proper format.</li></ol>

<div class="references_section">