S
S Jackson
I want to send the results of a query to a word document in the form of a
table. Originally, I tried setting up the word document as a merge
(catalogue) template file using the query as the data source. I built a
form in Access that requests that the user to select dates as parameters for
the query. The following is the code behind the "Enter" button on the form
which does not work since you get a message from Word saying the data source
is "in use" (Access is open when Word is attempting to open it as a data
source to complete the merge):
Private Sub cmdOk_Click()
On Error GoTo Err_cmdOk_Click
Dim stDocName As String
stDocName = "qryMonthEnd-Status"
DoCmd.OpenQuery stDocName
Dim objWord As Word.Application
Set objWord = New Word.Application
objWord.Documents.Add Application.CurrentProject.Path & "\Status_Rpt.dot"
objWord.Visible = True
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With
Windows("STATUS_RPT.dot").Activate
ActiveWindow.Close
'Release the Word object
Set objWord = Nothing
Exit_cmdOk_Click:
Exit Sub
Err_cmdOk_Click:
MsgBox Err.Description
Resume Exit_cmdOk_Click
End Sub
I have in the past figured out how to send the current record to Word using
automation by creating a word template, and inserting bookmarks. But in
this instance, I want to send all of the records contained in the query to
one Word document (a table). Do I have to transfer the query results to an
Excel spreadsheet and then use that as the Word data source? Seems like an
unnecessary extra step and there must be an easier way.
TIA
S. Jackson
table. Originally, I tried setting up the word document as a merge
(catalogue) template file using the query as the data source. I built a
form in Access that requests that the user to select dates as parameters for
the query. The following is the code behind the "Enter" button on the form
which does not work since you get a message from Word saying the data source
is "in use" (Access is open when Word is attempting to open it as a data
source to complete the merge):
Private Sub cmdOk_Click()
On Error GoTo Err_cmdOk_Click
Dim stDocName As String
stDocName = "qryMonthEnd-Status"
DoCmd.OpenQuery stDocName
Dim objWord As Word.Application
Set objWord = New Word.Application
objWord.Documents.Add Application.CurrentProject.Path & "\Status_Rpt.dot"
objWord.Visible = True
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With
Windows("STATUS_RPT.dot").Activate
ActiveWindow.Close
'Release the Word object
Set objWord = Nothing
Exit_cmdOk_Click:
Exit Sub
Err_cmdOk_Click:
MsgBox Err.Description
Resume Exit_cmdOk_Click
End Sub
I have in the past figured out how to send the current record to Word using
automation by creating a word template, and inserting bookmarks. But in
this instance, I want to send all of the records contained in the query to
one Word document (a table). Do I have to transfer the query results to an
Excel spreadsheet and then use that as the Word data source? Seems like an
unnecessary extra step and there must be an easier way.
TIA
S. Jackson