Microsoft KB Archive/132003

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

Article ID: 132003

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition

-



This article was previously published under Q132003



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:

 Create a form based on the table containing the MyOle field. Add a Bound Object Frame control to the form and set the control's properties as follows:

Name: MyOle

ControlSource: MyOle

  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   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  View the form in Form view. Click the CopyDocument button. Note that the embedded document is saved as Test.doc in your Microsoft Word folder.</ol>

<div class="references_section">