I posted a question earlier concerning a mail merge operation where I am adding the parameter into the mail merge directly:
sqlstatement:="SELECT * FROM [Customer Data]
WHERE [Customer Data].[Status]='Complete'
AND [Customer Data].[CompletedBy] = '" & userID & "'
AND [Customer Data].[Date Completed] Between #" & date1 & "#
And #" & date2 & "#;"
Changing SQL select query to VBA
It was suggested in the comments that this is a bad idea; I know this should be avoided for database operations because it can be exploited to run code altering the database, but is this a bad idea for printing mail merges? Could this result in a real-world exploit? It seems that SQL only chooses what will populate onto the letter merge. What other options are available in order to populate a mail merge correctly?
Set objword = GetObject("S:\Share with Bob\Administration 2010 Repository\Mail Merge\AIB.doc")
'Make Word visible
objword.Application.Visible = True
'Set the mail merge data source to the database
objword.MailMerge.OpenDataSource _
Name:="C:\Database.mdb", _
LinkToSource:=True, _
Connection:="[TABLE - TRANSACTION]", _
sqlstatement:="SELECT * FROM [TABLE - TRANSACTION] ORDER BY [TABLE - TRANSACTION].[Transaction Number]"
'Execute the mail merge
objword.MailMerge.Destination = wdSendToNewDocument
objword.MailMerge.Execute
objword.Application.Options.PrintBackground = False
'Print a copy
objword.Application.ActiveDocument.PrintOut
'Close Word and do not save the changes
objword.Application.Quit SaveChanges:=wdDoNotSaveChanges
'Clear the objWord Object
Set objword = Nothing