General discussion

Locked

Trouble with Basic VB code for Word Bookmarks

By Pete_Support ·
I am still very new at this..so 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
Err.Clear
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
.Activate
.Selection.WholeStory
.Selection.Fields.Update
.Selection.MoveDown Unit:=wdLine, Count:=1
End With

End Sub

This conversation is currently closed to new comments.

0 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Back to Software Forum
0 total posts (Page 1 of 1)  

Related Discussions

Related Forums