alpinegroove
Registered User.
- Local time
- Today, 01:37
- Joined
- May 4, 2011
- Messages
- 55
I am using code I pieced together from a few different sources online, but I am having trouble understanding how it works.
The main idea is to merge data from an Access query into a Word template. This specific method required me to first set up the mail merge in Word (designate data source and fields), and then run the code from Access.
But it looks like the Access code first copies the query results to a designated location in my network and if I understand it correctly, the Word template draws the data from that document rather than directly from the query, as it was set up to do in the mail merge wizard in Word.
Why does this code copy the data to an rtf format and saves it on the network rather than just tell word to execute the merge that had been set up?
Thank you.
The main idea is to merge data from an Access query into a Word template. This specific method required me to first set up the mail merge in Word (designate data source and fields), and then run the code from Access.
But it looks like the Access code first copies the query results to a designated location in my network and if I understand it correctly, the Word template draws the data from that document rather than directly from the query, as it was set up to do in the mail merge wizard in Word.
Why does this code copy the data to an rtf format and saves it on the network rather than just tell word to execute the merge that had been set up?
Thank you.
Code:
Private Sub Command1_Click()
Dim strPath As String
Dim strDataSource As String
Dim conTemplate As String
Dim conQuery As String
Dim doc As Word.Document
Dim wrdApp As Word.Application
conTemplate = "Contract Template.doc"
conQuery = "qryComp"
On Error GoTo HandleErrors
' Delete the rtf file, if it already exists.
strPath = "H:\"
strDataSource = strPath & conQuery & ".doc"
Kill strDataSource
' Export the data to rtf format.
DoCmd.OutputTo acOutputQuery, conQuery, _
acFormatRTF, strDataSource, False
' Start Word using the mail merge template.
Set wrdApp = New Word.Application
Set doc = wrdApp.Documents.Add(strPath & conTemplate)
' Do the mail merge to a new document.
With doc.MailMerge
.OpenDataSource Name:=strDataSource
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
If .State = wdMainAndDataSource Then
.Execute
End If
End With
' Display the mail merge document.
wrdApp.Visible = True
' Run Word macro that splits merge into separate documents and saves them
' File name comes from a catalog table prepared in advanced
' Word macro prompts user for the catalog file location.
With wrdApp
.Run "SplitMergedLetter"
.Quit
End With
ExitHere:
Set doc = Nothing
Set wrdApp = Nothing
Set doc2 = Nothing
Set wrdApp2 = Nothing
Exit Sub
HandleErrors:
Select Case Err.Number
Case 53 ' File not found.
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description
Resume ExitHere
End Select
End Sub