ACC: Sending the Current Record to Word with OLE AutomationID: Q124862
|
This article describes how you can merge the current record in a Microsoft
Access object into a document in Microsoft Word version 7.0 or earlier,
and open it in print preview.
For information about how to merge the current record with a Microsoft
Word 97 document, please see the following article in the Microsoft
Knowledge Base:
Q131583 ACC: Sending the Current Record to Word 97 with AutomationThis 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.
The following steps demonstrate how to create a Microsoft Word document and a Microsoft Access form, which are then used to merge the current record from Microsoft Access into Microsoft Word:
First Last
Address
City, Region, PostalCode
Dear Greeting,
Northwind Traders would like to thank you for
your employment during the past year. Below
you will find your photo. If this is not your
most current picture, please let us know.
Photo
Sincerely,
Northwind Traders
Dim Word As Object
In version 7.0:
Sub Command40_Click()
On Error GoTo CatchBlanks
Dim Word As Object ' Declare in the module to view the print
' preview after the Sub ends
DoCmd.GoToControl "Photo"
DoCmd.DoMenuItem acFormBar, acEditMenu, acCopy, , acMenuVer70
Set Word = CreateObject("Word.Basic")
Word.FileOpen ("C:\OLEMERGE.DOC")
Word.EditGoto "Last"
Word.INSERT CStr(Forms![Employees]![LastName])
Word.EditGoto "First"
Word.INSERT CStr(Forms![Employees]![FirstName])
Word.EditGoto "Address"
Word.INSERT CStr(Forms![Employees]![Address])
Word.EditGoto "City"
Word.INSERT CStr(Forms![Employees]![City])
Word.EditGoto "Region"
Word.INSERT CStr(Forms![Employees]![Region])
Word.EditGoto "PostalCode"
Word.INSERT CStr(Forms![Employees]![PostalCode])
Word.EditGoto "Greeting"
Word.INSERT CStr(Forms![Employees]![FirstName])
Word.EditGoto "Photo"
Word.EditPaste
'Word.FilePrint 0
' To send the record directly to the printer, unremark the line
' above, and remark the next two lines below.
Word.appmaximize "",1
Word.FilePrintPreview
Word.AppActivate "Microsoft Word"
Exit Sub
CatchBlanks:
If MsgBox("Error sending one field, it may be blank. Would _
you like to continue?", 52) = 6 Then
Resume Next
Else
Exit Sub
End If
End Sub
In version 2.0:
NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.
Sub Button182_Click ()
On Error GoTo CatchBlanks
Dim Word As Object ' Declare in the module to view the print
' preview after the Sub ends
DoCmd GoToControl "Photo"
DoCmd DoMenuItem A_FORMBAR, A_EDIT, A_COPY
Set Word = CreateObject("Word.Basic")
Word.FileOpen ("C:\OLEMERGE.DOC")
Word.EditGoto "Last"
Word.Insert CStr(Forms![Employees]![Last Name])
Word.EditGoto "First"
Word.Insert CStr(Forms![Employees]![First Name])
Word.EditGoto "Address"
Word.Insert CStr(Forms![Employees]![Address])
Word.EditGoto "City"
Word.Insert CStr(Forms![Employees]![City])
Word.EditGoto "Region"
Word.Insert CStr(Forms![Employees]![Region])
Word.EditGoto "PostalCode"
Word.Insert CStr(Forms![Employees]![Postal Code])
Word.EditGoto "Greeting"
Word.Insert CStr(Forms![Employees]![Last Name])
Word.EditGoto "Photo"
Word.EditPaste
' Word.FilePrint 0, 0, "0", "", "", "", 0, "1", "", 0, 0, 1, ""
' NOTE: When sending the current record from Microsoft Access 2.0
' to Microsoft Word 95, replace the above line with the
' following line:
' Word.FilePrint 0
' To send the record directly to the printer, unremark one of the
' lines above, and remark the next two lines below.
Word.appmaximize "",1
Word.FilePrintPreview
Word.AppActivate "Microsoft Word"
Exit Sub
CatchBlanks:
If MsgBox("Error sending one field, it may be blank. Would _
you like to continue?", 52) = 6 Then
Resume Next
Else
Exit Sub
End If
End Sub
Word.Insert Cstr(Format(Forms![FormName]![Price], [ASCII 147]Currency[ASCII 148]))
Additional query words: editgoto mail
Keywords : kbole kbprg kbdta AccCon KbVBA
Version : WINDOWS:2.0,7.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: August 2, 1999