Word

Get contacts data via DAO to document

Ease of Use

Intermediate

Version tested with

2003 

Submitted by:

MOS MASTER

Description:

Create a UserForm to get data from an access database in to bookmarks in the activedocument with DAO connection. 

Discussion:

You need to make a lot of letters to people. You already have a address database of those people. Still you?re typing out those names every time. With this solution you only have to choose the addressee and the code fills in the blanks for you. You can choose another contact after you picked the first one and have the current values overwritten. Also Fields are included for repeated values. 

Code:

instructions for use

			

'In module called AutoOpen Option Explicit 'Main Sub in AutoOpen module that will fire on opening a document Sub Main() frmContacts.Show End Sub Public Sub FillBookmark(sText As String, sBookmark As String) Dim oRange As Word.Range With Application.ActiveDocument 'Set oRange to start and end of bookmark sBookmark's range Set oRange = .Bookmarks(sBookmark).Range 'Fill the text of oRange with the value of sText oRange.Text = sText 'Create bookmark to range the text [ ] .Bookmarks.Add Name:=sBookmark, Range:=oRange 'Clean up Set oRange = Nothing End With End Sub 'In UserForm called frmContacts Option Explicit 'Add Reference to Microsoft DAO 3.6 Object Library Private Sub UserForm_Initialize() Dim dbs As DAO.Database Dim rst As DAO.Recordset 'Establish connection Set dbs = OpenDatabase(ActiveDocument.Path & "\Contacts.mdb") 'Fill recordset Set rst = dbs.OpenRecordset("Select Name FROM Contacts;") 'Fill combo by looping through the recordset Do While Not rst.EOF Me.cboContacts.AddItem rst("Name") rst.MoveNext Loop 'Clean up Set rst = Nothing Set dbs = Nothing End Sub Private Sub cmdInsert_Click() Dim dbs As DAO.Database Dim rst As DAO.Recordset 'If no selection was made exit sub If Me.cboContacts.ListIndex = -1 Then Beep MsgBox "Make a choice first", 64, "Choose from list" Exit Sub Else 'Hide form Me.Hide 'Establish connection Set dbs = OpenDatabase(ActiveDocument.Path & "\Contacts.mdb") 'Fill recordset with data matching the text of combo cboContacts Set rst = dbs.OpenRecordset("Select * FROM Contacts WHERE Name = '" & _ Me.cboContacts.Text & "';") 'Write values of Recordset fields to bookmarks in the document Call FillBookmark("" & rst.Fields("Company"), "bmCompany") Call FillBookmark("" & rst.Fields("Name"), "bmName") Call FillBookmark("" & rst.Fields("Address"), "bmAddress") Call FillBookmark("" & rst.Fields("Postal") & Chr$(32) & rst.Fields("City"), "bmCity") '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Call FillBookmark("" & rst.Fields("Phone"), "bmPhone") Call FillBookmark("" & rst.Fields("Mobile"), "bmMobile") Call FillBookmark("" & rst.Fields("Fax"), "bmFax") Call FillBookmark("" & rst.Fields("Website"), "bmwww") Call FillBookmark("" & rst.Fields("Email"), "bmEmail") Call FillBookmark("" & rst.Fields("Language"), "bmLanguage") Call FillBookmark("" & rst.Fields("Memo"), "bmMemo") ActiveDocument.Fields.Update 'Update linked fields End If 'Clean up Unload Me 'give resources back to the system Set rst = Nothing Set dbs = Nothing End Sub

How to use:

  1. Open your Word document.
  2. Press Alt + F11 to open VBE.
  3. Insert-Module. (Insert -> module)
  4. Name it: AutoOpen
  5. Copy the code from above from part: ?In module called AutoOpen?
  6. Paste the code module AutoOpen. (F7)
  7. Insert-UserForm (Insert -> UserForm)
  8. Name the Userform: frmContacts
  9. Place Combobox on it called: cboContacts
  10. Place Commandbutton on it called: cmdInsert
  11. Double click the Userform delete any code left and paste the code from above from part: In UserForm called frmContacts
  12. Add Reference to Microsoft DAO 3.6 Object Library (Tools/Add reference)
  13. Close VBE (Alt + Q or press the X in the top right hand corner).
  14. Add 11 bookmarks to the document (Insert/Bookmark)
  15. Call them:
  16. bmAddress
  17. bmCity
  18. bmCompany
  19. bmEmail
  20. bmFax
  21. bmLanguage
  22. bmMemo
  23. bmMobile
  24. bmName
  25. bmPhone
  26. bmwww
  27. For each bookmark value that needs to be repeated add a Field to the location (CTRL+F9) and type the bookmark name in that field. Update the field (F9)
  28. Save the file.
  29. Close the file
 

Test the code:

  1. Open the Word document (AutoOpen will run now)
  2. Choose a name
  3. Press button: Insert
  4. The data is filled in the document
  5. For repeated use press the commandbutton on the Toolbar called: Choose other contact
 

Sample File:

DAO.zip 23.88KB 

Approved by mdmackillop


This entry has been viewed 183 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express