make a button do two things in access vba

historyb

Registered User.
Local time
Today, 08:30
Joined
Dec 24, 2011
Messages
12
I am a newbie at VBA. I have the code for my button to do one thing and now I am stuck. How do I get one button to fill in bookmarks on different documents. Here is my code and it is not working

Code:
Option Compare Database

Private Sub cmd_Word_Click()

'Declare Variables
Dim sAccessAddress As String
Dim Salutation As String

'Build sAccessAddress
sAccessAddress = FirstName & " " & LastName & vbCrLf & Address & vbCrLf & StateOrProvince & " " & Zipcode

'Build sAccessSalutation
Salutation = FirstName & " " & LastName

'Declare and set Word object variables
Dim Wrd As Word.Application
Set Wrd = CreateObject("Word.Application")

'Specify Path to Template
Dim sMergeDoc As String
Dim sMerge As String
sMergeDoc = Application.CurrentProject.Path & "\WordMergeDocument.dotx"
sMerge = Application.CurrentProject.Path & "C:\Users\dwilson\Desktop\Active Work\CertofRehab.dotx"

'Open Word using template and make Word visible
Wrd.Documents.Add sMergeDoc
Wrd.Documents.Add sMerge
Wrd.Visible = True

'Replace Bookmarks with Values
With Wrd.ActiveDocument.Bookmarks
.Item("CurrentDate").Range.Text = Date
.Item("AccessAddress").Range.Text = sAccessAddress
.Item("Salutation").Range.Text = sAccessSalutation
End With

'Open in PrintPreview mode, let user print
Wrd.ActiveDocument.PrintPreview

'Clean Up code
Set Wrd = Nothing
End Sub
 
When you say it is not working, is it not working for a single document? Does it fail within the code pasted above? If so, where?

If you want it to fill in bookmarks in different documents, rather than pointing to a specific Document, you will need to point to a folder and then set up a loop in your code to loop through each file and change the bookmarks for each.
 
Sorry about not pointing out the error. I thought I did, but see I didn't. Here is where the error occurs:

Code:
'Open Word using template and make Word visible
Wrd.Documents.Add sMergeDoc
Wrd.Documents.Add sMerge <-- [COLOR=Blue][B]Here is where the error is thrown[/B]
[/COLOR]Wrd.Visible = True

So would I need a loop for this section also:

Code:
'Replace Bookmarks with Values
With Wrd.ActiveDocument.Bookmarks
.Item("CurrentDate").Range.Text = Date
.Item("AccessAddress").Range.Text = sAccessAddress
.Item("Salutation").Range.Text = sAccessSalutation
End With

because not all the bookmarks may be in very document.
 

Users who are viewing this thread

Back
Top Bottom