ACC95: How to Use OLE Automation to Create a Word Document
ID: Q142475
|
The information in this article applies to:
SUMMARY
Moderate: Requires basic macro, coding, and interoperability skills.
This article demonstrates how to use OLE Automation to create and
manipulate a Microsoft Word document.
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.
MORE INFORMATION
How to Create a New Microsoft Word Document
- Create a new folder called Examples on drive C.
- Open a Microsoft Access 7.0 database and create a new module.
- Type the following function in the Module window:
Public Function wordtest()
Dim w As Object
Set w = CreateObject("word.basic")
With w
.filenew template:="Normal"
.INSERT "This is a test."
.editselectall
.allcaps 1
.bold 1
.formatfont points:=14, underline:=1, font:="arial"
.filesaveas Name:="c:\examples\wordtest.doc"
End With
End Function
- Press CTRL+G to open the Debug window and type the following word:
wordtest
- Start Microsoft Word 7.0, and view the new file
c:\examples\wordtest.doc.
How to Create a New Microsoft Word Document Using a Bound Form Control
- Create a new table and name it Test Table.
- Add a MyOleField field as an Ole Object data type.
- Create a new form based on the Test Table table by using the
AutoForm: Columnar Wizard.
- Add a command button to the form with the following properties:
Name: Command4
Caption: My Button
OnClick: [Event Procedure]
- Enter the following code for the command button's OnClick property event
procedure:
Private Sub Command4_Click()
Dim worddoc As Object
With MyOleField
.Class = "Word.Document"
.OLETypeAllowed = acOLEEmbedded
.Action = acOLECreateEmbed
.Verb = acOLEVerbInPlaceUIActivate
.Action = acOLEActivate
End With
Set worddoc = Me![myolefield].Object.Application.wordbasic
With worddoc
.INSERT "This is a test."
.editselectall
.allcaps 1
.bold 1
.formatfont points:=14, underline:=1, font:="arial"
End With
Command4.SetFocus
End Sub
- Switch the form to Form view and click the My Button button. Note that
the data formatted by Microsoft Word is inserted into the OLE Field.
REFERENCES
For more information about creating objects with OLE Automation, search on
the phrase "Create a Word Document using OLE Automation," and then
"CreateObject function" using the Microsoft Access for Windows 95 Answer
Wizard.
Microsoft Access, "Building Applications with Microsoft Access for Windows
95, version 7.0, Chapter 11, "Communicating with Other Applications," pages
262-290.
Additional query words:
Keywords : kbole IntpOlea
Version : WINDOWS:7.0
Platform : WINDOWS
Issue type : kbhowto