General discussion


Trouble with Basic VB code for Word Bookmarks

By Pete_Support ·
I am still very new at please bare with me.
I have been trying to write an access code to populate a word document with a table from access with no luck, I have been able to open the correct file but it will not populate. Can someone please tell me what I have missed? Thanks
Here is my code:
Private Sub Command9_Click()

Dim appWord As Object
Set appWord = GetObject(, "Word.Application")

If Err Then
Set appWord = CreateObject("Word.Application")
If Err Then
MsgBox "Can't start Word!"
Exit Sub
End If
appWord.Visible = True
End If
appWord.Documents.Open ("C:\Users\ Documents\ Practice Sample\Bookmark.docx")

Dim Wrd As Object
Set Wrd = CreateObject("Word.Application")
Dim Mergedoc As String
Mergedoc = Application.CurrentProject.Path
Mergedoc = Mergedoc + "Bookmark.docx"
Wrd.Documents.Add Mergedoc
Wrd.Visible = True

With Wrd.ActiveDocument.Bookmarks
.ActiveDocument.Bookmarks("Name").Value = "(Me![Name])"
.ActiveDocument.Bookmarks("Name").Value = Nz(Me![Name])
.ActiveDocument.Bookmarks("SpouseName").Value = Nz(Me![SpouseName])
.ActiveDocument.Bookmarks("Address").Value = Nz(Me![Address])
.ActiveDocument.Bookmarks("FamilyName").Value = Nz(Me![FamilyName])
End With

With appWord
.Visible = True
.Selection.MoveDown Unit:=wdLine, Count:=1
End With

End Sub

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Related Discussions

Related Forums