I use the following code to achieve following
a) Open a word document from MS access / VBA,
b) Connect to data from a query
c) mailmerge it
d) Save it as a new document containing mailemerged letters.
The code does open the required document as I can see it open.
I put a message box to recheck that filename/path is what I mean, and to to know till which line the code runs. Code runs till that point and that word doc is seen open. I get the error message before
Then I get error message.
I was fooling around and was getting message " This command is not available because no document is open" I tried to remove few lines thinking redundancy. Before getting the message, I was also getting the error trapping message "No documents opened."
Now I am getting different message.
I am getting message " The remote Server Machine does not Exist or is unavailable"
Following is the code I have written.
I accept the limitation of self taught programmer using copy/ paste quite a bit, and limitations of understanding coming with it.
a) Open a word document from MS access / VBA,
b) Connect to data from a query
c) mailmerge it
d) Save it as a new document containing mailemerged letters.
The code does open the required document as I can see it open.
I put a message box to recheck that filename/path is what I mean, and to to know till which line the code runs. Code runs till that point and that word doc is seen open. I get the error message before
Then I get error message.
I was fooling around and was getting message " This command is not available because no document is open" I tried to remove few lines thinking redundancy. Before getting the message, I was also getting the error trapping message "No documents opened."
Now I am getting different message.
I am getting message " The remote Server Machine does not Exist or is unavailable"
Following is the code I have written.
I accept the limitation of self taught programmer using copy/ paste quite a bit, and limitations of understanding coming with it.
Code:
Dim xlApp As Object
Set xlApp = CreateObject("word.Application")
xlApp.Visible = True
fn = FormPathName("941 2014 AutoOpen.doc")
Dim wdocSource As Object
Set wdocSource = xlApp.Documents.Open(fn)
wdocSource.MailMerge.OpenDataSource _
name:=getSystemPref("mdbwithPathForMailmergeDataSource"), SQLStatement:="SELECT * FROM [941Table]"
xlApp.Documents.Open (fn)
xlApp.Documents.Open(fn).Activate ' This may be redundant but was part of my trial error
'-- Following loop was added thinking that I am trying to see the Active 'Document before it really opens. Just a delay
Do While k < 40000
k = k + 1
Loop
MsgBox (fn)
' File Name is displayed correctly
' The Word doc is seen open
' Connection is good as I can manually see the mailemerged data.
If Documents.count > 0 Then
' MsgBox ActiveDocument.name
Else
MsgBox ("No documents opened.")
End If
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With
'
Dim sSaveName
sSaveName = getSystemPref("OutputFiles") & "941s Q" & q & " " & getSystemPref("CurrentYear") & ".doc"
Documents(1).SaveAs (sSaveName)
'ActiveDocument.Close
Documents("941 2014 AutoOpen.doc").Close
xlApp.Visible = True
xlApp.Activate
Set xlApp = Nothing
[\CODE]
I have used lot of information from the forum before and really appreciate the help.
Thanks
jd1963