I am trying to create a button on a form that would simple copy the data of a query to excel and automtically saves the file to the c drive as qrylabels then execute the following code using the qrylabel excel file as the datasource for a mailmerge in word.
Private Sub bttnWord_Click()
Dim LWordDoc As String
Dim oApp As Object
'Path to the word document
LWordDoc = "C:\MailMergeLabels.doc"
If Dir(LWordDoc) = "" Then
MsgBox "Document not found."
Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
'Open the Document
oApp.Documents.Open filename:=LWordDoc
oApp.OpenDataSource Name:="C:\qrylabels.xls"
oApp.ActiveDocument.MailMerge.Execute 0
oApp.ActiveDocument.Fields.Update
End If
End Sub
Every single time the user clicks on the button it should write over the existing one and remerge. I am using ADP with SQL. From word the user then decides to view the file, make some minor changes if needed and then print. Problem is I know I am missing a few lines of code here and have no clue what those are. Please help!!
Private Sub bttnWord_Click()
Dim LWordDoc As String
Dim oApp As Object
'Path to the word document
LWordDoc = "C:\MailMergeLabels.doc"
If Dir(LWordDoc) = "" Then
MsgBox "Document not found."
Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
'Open the Document
oApp.Documents.Open filename:=LWordDoc
oApp.OpenDataSource Name:="C:\qrylabels.xls"
oApp.ActiveDocument.MailMerge.Execute 0
oApp.ActiveDocument.Fields.Update
End If
End Sub
Every single time the user clicks on the button it should write over the existing one and remerge. I am using ADP with SQL. From word the user then decides to view the file, make some minor changes if needed and then print. Problem is I know I am missing a few lines of code here and have no clue what those are. Please help!!