ACC2000: Sending the Current Record to Word 2000 with AutomationID: q210271
|
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article shows you how to merge the current record in a Microsoft
Access object into a Microsoft Word document, to print it, and then to close Microsoft Word.
Microsoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.aspMicrosoft Word version 97 and later use the Visual Basic for Applications programming model, instead of the WordBasic flat command model used in some of the earlier versions.
Q124862 ACC: Sending the Current Record to Word with OLE AutomationThe following example uses bookmarks in a Microsoft Word document to mark the locations where you want to place data from a record on a Microsoft Access form.
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
Name: MergeButton
Caption: Send to Word
Private Sub MergeButton_Click()
On Error GoTo MergeButton_Err
Dim objWord As Word.Application
'Copy the Photo control on the Employees form.
DoCmd.GoToControl "Photo"
DoCmd.RunCommand acCmdCopy
'Start Microsoft Word 97.
Set objWord = CreateObject("Word.Application")
With objWord
'Make the application visible.
.Visible = True
'Open the document.
.Documents.Open ("C:\MyMerge.doc")
'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("First").Select
.Selection.Text = (CStr(Forms!Employees!FirstName))
.ActiveDocument.Bookmarks("Last").Select
.Selection.Text = (CStr(Forms!Employees!LastName))
.ActiveDocument.Bookmarks("Address").Select
.Selection.Text = (CStr(Forms!Employees!Address))
.ActiveDocument.Bookmarks("City").Select
.Selection.Text = (CStr(Forms!Employees!City))
.ActiveDocument.Bookmarks("Region").Select
.Selection.Text = (CStr(Forms!Employees!Region))
.ActiveDocument.Bookmarks("PostalCode").Select
.Selection.Text = (CStr(Forms!Employees!PostalCode))
.ActiveDocument.Bookmarks("Greeting").Select
.Selection.Text = (CStr(Forms!Employees!FirstName))
'Paste the photo.
.ActiveDocument.Bookmarks("Photo").Select
.Selection.Paste
End With
'Print the document in the foreground so Microsoft Word will not close
'until the document finishes printing.
objWord.ActiveDocument.PrintOut Background:=False
'Close the document without saving changes.
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Quit Microsoft Word and release the object variable.
objWord.Quit
Set objWord = Nothing
Exit Sub
MergeButton_Err:
'If a field on the form is empty, remove the bookmark text, and
'continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
'If the Photo field is empty.
ElseIf Err.Number = 2046 Then
MsgBox "Please add a photo to this record and try again."
Else
MsgBox Err.Number & vbCr & Err.Description
End If
Exit Sub
End Sub
.ActiveDocument.Bookmarks("Last").Select
.Selection.Text = (CStr(Forms!Employees!LastName))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Last",Range:=Selection.Range
This macro could be used to simulate a mailmerge with Word that includes a picture field from an Access record, by placing the code above in a For...Next, While...Wend, or For...Each Loop.
For more information about Automation between Microsoft Access and Microsoft Word, click Microsoft Access Help on the
Help menu, type "sharing data between applications, Microsoft Word mail merge data." in the Office Assistant or the Answer Wizard,
and then click Search to view the topics returned.
For more information about bookmarks, click Microsoft Word Help on the
Help menu, type "bookmarks" in the Office Assistant or the Answer Wizard,
and then click Search to view the topics returned.
Additional query words:
Keywords : kbmacro kbole kbprg AccCon word8 IntpOlea
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: July 6, 1999