U
Ultraviolet47
Hello everyone.
I have used a table as a datasource to mail merge in Word 2003 from
Access 2003. Now I need to change this to a query. I have altered the
code that references the table and changed it to the query name
(Qry_WelcomeLetter), but it says Word cannot find the data source.
Another curious thing that happens is when I execute the merge via this
code, before it debugs to say it can't find the database, it asks me to
login as a user. I have gotten this message previously, but thought
that was because I had not set Word to become visible. I then added the
code to make Word visible and after a few weeks of peace it's asking me
for a user name and password for Admin again?
ANy ideas to stop the username request and how to get it to recognise
the data souce?
Here is my code:
Private Sub Print_Welcome_Letter_Click()
Dim objWord As Object
Set objWord = GetObject("C:\Documents and Settings\Stel\My
Documents\Cavy\Sponsorship pack\Sponsors\welcome letter.doc",
"Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the Rat Rescue database.
objWord.MailMerge.OpenDataSource _
Name:="C:\Documents and Settings\Stel\My
Documents\Cavy\Sponsorship pack\Sponsors\CavyRescue.mdb", _
LinkToSource:=True, _
Connection:="QUERY Qry_WelcomeLetter", _
SQLStatement:="SELECT * FROM Qry_WelcomeLetter WHERE SponsorID =
(SELECT Max(SponsorID) FROM Qry_WelcomeLetter)"
objWord.MailMerge.Destination = SendToNewDocument
objWord.MailMerge.Execute
'The following line must follow the Execute statement because the
'PrintBackground property is available only when a document window is
'active. Without this line of code, the function will end before Word
'can print the merged document.
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
End Sub
I have used a table as a datasource to mail merge in Word 2003 from
Access 2003. Now I need to change this to a query. I have altered the
code that references the table and changed it to the query name
(Qry_WelcomeLetter), but it says Word cannot find the data source.
Another curious thing that happens is when I execute the merge via this
code, before it debugs to say it can't find the database, it asks me to
login as a user. I have gotten this message previously, but thought
that was because I had not set Word to become visible. I then added the
code to make Word visible and after a few weeks of peace it's asking me
for a user name and password for Admin again?
ANy ideas to stop the username request and how to get it to recognise
the data souce?
Here is my code:
Private Sub Print_Welcome_Letter_Click()
Dim objWord As Object
Set objWord = GetObject("C:\Documents and Settings\Stel\My
Documents\Cavy\Sponsorship pack\Sponsors\welcome letter.doc",
"Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the Rat Rescue database.
objWord.MailMerge.OpenDataSource _
Name:="C:\Documents and Settings\Stel\My
Documents\Cavy\Sponsorship pack\Sponsors\CavyRescue.mdb", _
LinkToSource:=True, _
Connection:="QUERY Qry_WelcomeLetter", _
SQLStatement:="SELECT * FROM Qry_WelcomeLetter WHERE SponsorID =
(SELECT Max(SponsorID) FROM Qry_WelcomeLetter)"
objWord.MailMerge.Destination = SendToNewDocument
objWord.MailMerge.Execute
'The following line must follow the Execute statement because the
'PrintBackground property is available only when a document window is
'active. Without this line of code, the function will end before Word
'can print the merged document.
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
End Sub