Microsoft KB Archive/210271

From BetaArchive Wiki

Article ID: 210271

Article Last Modified on 10/11/2006



APPLIES TO

  • Microsoft Access 2000 Standard Edition
  • Microsoft Word 2000 Standard Edition



This article was previously published under Q210271

For a Microsoft Access 97 version of this article, see 131583.

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


SUMMARY

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.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. Microsoft 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.

Creating a Microsoft Word Document

  1. Start Microsoft Word and create the following new document:

    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

  2. Create a bookmark in Microsoft Word for the words "First," "Last," "Address," "City," "Region," "PostalCode," "Greeting," and "Photo." To do so, follow these steps:
    1. Highlight the word "First," and then press CTRL+C.
    2. On the Insert menu, click Bookmark.
    3. Click in the Bookmark Name box, press CTRL+V, and then click Add.
    4. Repeat steps a through c for each of the remaining words, substituting that word for the word "First" in steps a and c.
  3. Save the document as C:\MyMerge.doc, and then quit Microsoft Word.

Sending Data to Microsoft Word from a Microsoft Access Form

  1. Start Microsoft Access and open the sample database Northwind.mdb.
  2. Set a reference to the Microsoft Word 9.0 Object Library. To do so, follow these steps:
    1. Open any module in Design view.
    2. On the Tools menu, click References.
    3. Click to select the Microsoft Word 9.0 Object Library check box in the Available References box. If that selection does not appear, browse for Msword9.olb, which is installed by default in the C:\Program Files\Microsoft Office\Office folder.
    4. Click OK.
    5. Close the module.
  3. Open the Employees form in Design view.
  4. Add a command button to the form and set the following properties:

       Name: MergeButton
       Caption: Send to Word
                        
  5. Set the OnClick property of the command button to the following event procedure:

    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
                        
  6. Save the Employees form and open it in Form view.
  7. Click Send To Word to start Microsoft Word, to merge data from the current record on the form into MyMerge.doc, to print the document, and then to close Microsoft Word.

NOTE: When you use this method of inserting text into a Word Document, you are deleting the bookmark when you insert the record field content. If you need to reference the text that you entered into the document, you must bookmark it. You can use the following sample to add the bookmark "Last" to the text inserted from record field "LastName."

.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.

REFERENCES

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.


Keywords: kbautomation kbfaq kbhowto kbmacro kbprogramming KB210271