PRB: SaveToFile Method Does Not Save Embedded Document in Proper Format (313812)



The information in this article applies to:

  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0

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 "C:\MyFile.doc"

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the behavior
  1. Open Visual Basic 6.0 IDE. On File menu, click New Project.
  2. On the New Project dialog box, select Standard Exe and then click OK. By default Form1 is displayed.
  3. From the Toolbox drag OLE control and drop it onto the Form1.
  4. In the Insert Object dialog box, select Microsoft Word Document Object Type and then click OK.
  5. Type some text in the Word document contained in OLE container.
  6. 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.
  7. Add the following code to cmdSave_Click event:
       Dim FileNum As Integer
       ' Get file number.
       FileNum = FreeFile
       ' Open file to be saved.
       Open "C:\MyFile.doc" For Binary As #FileNum
       ' Save the changes the file.
       OLE1.SaveToFile FileNum
       ' Close the file.
       Close #FileNum
  8. On Run menu, click Start to run the application.
  9. Click Save .
  10. Open the C:\MyFile.doc in Word Application. The Word Document will not open in proper format.
  11. Replace the code in cmdSave_Click event with the following code:
       ' Save the file using contained application method
       OLE1.object.SaveAs ("C:\MyNewFile.doc")
  12. Repeat step 8, 9and 10. This time the Word document opens in proper format.

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

242243 HOWTO: Embed and Automate Office Documents with Visual Basic


For additional information, click the following links at Microsoft Site
SaveToFile method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vb98/html/vbmthsavetofile.asp
Saving and Retrieving Embedded Data
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconsavingretrievingembeddeddata.asp

Modification Type:MinorLast Reviewed:4/24/2003
Keywords:kbOLEApp kberrmsg kbprb KB313812 kbAudDeveloper