Object Property Example

The following example uses the Object property of an unbound object frame named OLE1. Customer name and address information is inserted in an embedded Microsoft Word document formatted as a form letter with placeholders for the name and address information and boilerplate text in the body of the letter. The procedure replaces the placeholder information for each record and prints the form letter. It doesn't save copies of the printed form letter.

Sub PrintFormLetter_Click()
    Dim objWord As Object
    Dim strCustomer As String, strAddress As String
    Dim strCity As String, strRegion As String

    ' Assign object property of control to variable.
    Set objWord = Me!OLE1.Object.Application.Wordbasic
    ' Assign customer address to variables.
    strCustomer = Me!CompanyName
    strAddress = Me!Address
    strCity = Me!City & ", "
    If Not IsNull(Me!Region) Then
        strRegion = Me!Region
    Else
        strRegion = Me!Country
    End If
    ' Activate ActiveX control.
    Me!OLE1.Action = acOLEActivate
    With objWord
        .StartOfDocument
        ' Go to first placeholder.
        .LineDown 2
        ' Highlight placeholder text.
        .EndOfLine 1
        ' Insert customer name.
        .Insert strCustomer
        ' Go to next placeholder.
        .LineDown
        .StartOfLine
        ' Highlight placeholder text.
        .EndOfLine 1
        ' Insert address.
        .Insert strAddress
        ' Go to last placeholder.
        .LineDown
        .StartOfLine
        ' Highlight placeholder text.
        .EndOfLine 1
        ' Insert City and Region.
        .Insert strCity & strRegion
        .FilePrint
        .FileClose
    End With
    Set objWord = Nothing
End Sub