Thread: [Solved] Word Doc Macro (mail Merge)
View Single Post
 
Old 05-10-2011, 10:15 PM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2007
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi ajolson,

You can execute the mailmerge automatically by making the macro an AutoOpen macro. Without knowing more about the error you referred to, it's difficult to advise. What did the error message say? When you check the code, what line is highlighted?

On the assumption it's an error related to trying to close the file, try the following:

Code:
Sub AutoOpen()
Dim StrConnection As String, MailMergeMainDoc As Document
Set MailMergeMainDoc = ActiveDocument
With MailMergeMainDoc.MailMerge
  StrConnection = "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=F:\DB_Docs\DB_2010\PI_Sys.accdb;"
  StrConnection = StrConnection & "Mode=Read;E xtended Properties="""";Jet OLEDB:System database="""";"
  StrConnection = StrConnection & "Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=6;"
  StrConnection = StrConnection & "Jet OLEDBatabase Locking Mode=1;Jet OLEDB:Global"
  .MainDocumentType = wdFormLetters
  .OpenDataSource Name:="F:\DB_Docs\DB_2010\PI_Sys.accdb", ConfirmConversions:=False, _
  ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:="", _
  PasswordTemplate:="", WritePasswordDocument:="", WritePasswordTemplate:="", _
  Revert:=False, Format:=wdOpenFormatAuto, Connection:=StrConnection, _
  SQLStatement:="SELECT * FROM `TY_Letter`", SQLStatement1:="", SubType:=wdMergeSubTypeAccess
  .Destination = wdSendToNewDocument
  .SuppressBlankLines = True
  With .DataSource
    .FirstRecord = wdDefaultFirstRecord
    .LastRecord = wdDefaultLastRecord
  End With
  .Execute Pause:=False
  .Close SaveChanges:=False
End With
End Sub
PS: When posting code, please use code tags. You'll also note I've broken up your Connection line into a series of shorter strings. That should make it easier to review & maintain the code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote