ACC: How to Save a Copy of an Embedded MS Word Document

ID: Q132003


The information in this article applies to:
  • Microsoft Access versions 2.0, 7.0


SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article demonstrates how you can save a copy of an embedded Microsoft Word for Windows document (version 6.0 or later) using a form and OLE Automation in Microsoft Access.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications With Microsoft Access For Windows 95" manual.

NOTE: Visual Basic for Applications (used in Microsoft Access version 7.0) is called Access Basic in version 2.0. For more information about Access Basic, please refer to the "Building Applications" manual.


MORE INFORMATION

The following example assumes that you have a Microsoft Access table with an OLE Object field called MyOle that contains embedded (not linked) Microsoft Word documents.

To save a copy of an embedded Microsoft Word document, follow these steps:

  1. Create a form based on the table containing the MyOle field.


  2. Add a Bound Object Frame control to the form and set the control's properties as follows:
    Name: MyOle
    ControlSource: MyOle


  3. Add a command button named CopyDocument and set the control's OnClick property to the following event procedure:
    
          Sub CopyDocument_Click ()
              Dim NewObject As Object
              Dim NewDoc As String
              Dim DocPath As String
    
              ' Name of the new document to create.
              NewDoc = "TEST.DOC"
    
              ' Where to store the new document.
              ' DefaultDir$(9) returns the Word directory path.
              ' See DefaultDir$() in Word's on-line help for more options.
              ' Note: The "$" is not used when calling DefaultDir via
              ' OLE Automation.
              DocPath = Me!MyOle.Object.Application.WordBasic.DefaultDir(9)
    
              ' Copies the embedded object to Clipboard.
              Me!MyOle.Verb = 0
              Me!MyOle.Action = 7
              Me!MyOle.Object.Application.WordBasic.EditSelectAll
              Me!MyOle.Object.Application.WordBasic.EditCopy
              Me!MyOle.Action = 9
              DoEvents
    
              ' Creates a new document and pastes Clipboard contents.
              ' Saves the document in the Word directory and closes the
              ' document.
              Set NewObject = CreateObject("Word.Basic")
              NewObject.FileNew
              NewObject.EditPaste
              NewObject.FileSaveAs DocPath & "\" & NewDoc
              NewObject.FileClose
    
              ' Frees the memory used by the objects.
              Set NewObject = Nothing
              MsgBox DocPath & "\" & NewDoc & " was created successfully."
          End Sub 


  4. Type the following line in the Declarations section of the form's module if it's not already there, and then close the module:
    
          Option Explicit 


  5. View the form in Form view.


  6. Click the CopyDocument button. Note that the embedded document is saved as Test.doc in your Microsoft Word folder.



REFERENCES

For more information about OLE Automation, search for OLE Automation using the Microsoft Access for Windows 95 Help Index.

For more information about WordBasic commands available through OLE Automation, search for WordBasic using the Microsoft Word for Windows Help menu.

Additional query words:

Keywords : FmsHowto IntpOlea
Version : WINDOWS:2.0,7.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: October 14, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.