Load contact table from Access and create letter in Word : Word Document « Word « VBA / Excel / Access / Word






Load contact table from Access and create letter in Word

 
Sub ControlWord()
    Dim objWord As New Word.Application
    Dim rsContacts As New ADODB.Recordset
    Dim strLtrContent As String
    rsContacts.ActiveConnection = CurrentProject.Connection
    rsContacts.Open "tblContacts"
    
    objWord.Documents.Add
    
    Do While Not rsContacts.EOF
      strLtrContent = rsContacts("FirstName") & " " & rsContacts("LastName")
      strLtrContent = strLtrContent & rsContacts("Address") & vbCrLf
      strLtrContent = strLtrContent & rsContacts("City") & ", " & rsContacts("Region")
      strLtrContent = strLtrContent & "  " & rsContacts("PostalCode") 
      strLtrContent = strLtrContent & "Dear " & rsContacts("FirstName") & " "
      strLtrContent = strLtrContent & rsContacts("LastName") & ":" 
    
        objWord.Selection.EndOf
        objWord.Selection.Text = strLtrContent
    
        objWord.Selection.EndOf
        objWord.Selection.InsertBreak
        
        rsContacts.MoveNext
    Loop
    objWord.Visible = True
    objWord.PrintPreview = True
End Sub

 








Related examples in the same category

1.Add a word document
2.To create a new document that uses a specific template, use this:
3.Open an Existing Document
4.Save Changes to a Document
5.To save a document with a new name, use the SaveAs method
6.To close a specific document, you can close the active document or you can specify a document name:
7.Save a document
8.Generating Word 2000 data from an Excel VBA program
9.Close a document