J
Jason
Hi,
Can somebody help me with my code? I would like to do a mailmerge through
document variables, which
i've created in word.
I'm using a continueous form in access and a commandbutton to do a
mailmerge.
I can manage it to do it for one record, but not for multiple records.
This is what i would to do:
- if someone wants to merge 1 record then the code should call word, by
using
document properties or variables and create a document based on the
template.
-if all or a selection of records has been choosen then it should do like
above,
but then reproduce the same document in one file (otherwise they have to
print
all documents separatelately, rather then just press print).
Here's my code:
On Error GoTo ErrorHandler
Dim appWord As Word.Application
Dim docs As Word.Documents
Dim strLetter As String
Dim strTemplateDir As String
Dim prps As Object
Dim strDate As String
Set appWord = GetObject(, "Word.Application")
strDate = CStr(Date)
strTemplateDir = appWord.Options.DefaultFilePath(wdUserTemplatesPath)
strTemplateDir = strTemplateDir & "\"
'Debug.Print "Office templates directory: " & strTemplateDir
strLetter = strTemplateDir & "DocProps.dot"
'Debug.Print "Letter: " & strLetter
Set docs = appWord.Documents
docs.Add strLetter
Set prps = appWord.ActiveDocument.CustomDocumentProperties
'There should be a way to reference the prps in a for each control in my
form, but i don't know how to do it. So it did it like this.
With prps
.Item("TodayDate").Value = strDate
.Item("Address").Value = Nz(Me![txtAddress])
.Item("Salutation").Value = Nz(Me![txtSalutation])
.Item("CompanyName").Value = Nz(Me![txtCompanyName])
.Item("City").Value = Nz(Me![txtCity])
.Item("StateProv").Value = Nz(Me![txtStateOrProvince])
.Item("PostalCode").Value = Nz(Me![txtPostalCode])
.Item("JobTitle").Value = Nz(Me![txtTitle])
End With
With appWord
.Visible = True
.Activate
.Selection.WholeStory
.Selection.Fields.Update
.Selection.MoveDown Unit:=wdLine, Count:=1
End With
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If
So if anyone can adjust or improve my code, I would be very happy.
Can somebody help me with my code? I would like to do a mailmerge through
document variables, which
i've created in word.
I'm using a continueous form in access and a commandbutton to do a
mailmerge.
I can manage it to do it for one record, but not for multiple records.
This is what i would to do:
- if someone wants to merge 1 record then the code should call word, by
using
document properties or variables and create a document based on the
template.
-if all or a selection of records has been choosen then it should do like
above,
but then reproduce the same document in one file (otherwise they have to
all documents separatelately, rather then just press print).
Here's my code:
On Error GoTo ErrorHandler
Dim appWord As Word.Application
Dim docs As Word.Documents
Dim strLetter As String
Dim strTemplateDir As String
Dim prps As Object
Dim strDate As String
Set appWord = GetObject(, "Word.Application")
strDate = CStr(Date)
strTemplateDir = appWord.Options.DefaultFilePath(wdUserTemplatesPath)
strTemplateDir = strTemplateDir & "\"
'Debug.Print "Office templates directory: " & strTemplateDir
strLetter = strTemplateDir & "DocProps.dot"
'Debug.Print "Letter: " & strLetter
Set docs = appWord.Documents
docs.Add strLetter
Set prps = appWord.ActiveDocument.CustomDocumentProperties
'There should be a way to reference the prps in a for each control in my
form, but i don't know how to do it. So it did it like this.
With prps
.Item("TodayDate").Value = strDate
.Item("Address").Value = Nz(Me![txtAddress])
.Item("Salutation").Value = Nz(Me![txtSalutation])
.Item("CompanyName").Value = Nz(Me![txtCompanyName])
.Item("City").Value = Nz(Me![txtCity])
.Item("StateProv").Value = Nz(Me![txtStateOrProvince])
.Item("PostalCode").Value = Nz(Me![txtPostalCode])
.Item("JobTitle").Value = Nz(Me![txtTitle])
End With
With appWord
.Visible = True
.Activate
.Selection.WholeStory
.Selection.Fields.Update
.Selection.MoveDown Unit:=wdLine, Count:=1
End With
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If
So if anyone can adjust or improve my code, I would be very happy.