Mailmerge automation

JeremySun

Board Regular
Joined
Jul 1, 2011
Messages
98
I have a code below that should open a word template and start mail merge. So far all it does is open word and stops. Please help

Code:
Private Sub CommandButton1_Click()
 Dim wdApp As Word.Application
Dim WordWasNotRunning As Boolean
Dim wdDoc As Word.Document
 
 'Get existing instance of Word if it's open; otherwise create a new one
 
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
    Set wdApp = New Word.Application
    WordWasNotRunning = True
End If
 
wdApp.Visible = True
wdApp.Activate
Dim t
t = "AHLTA.dotx"
Set wdDoc = wdApp.Documents.Add(t)
 
Dim sql
sql = "SELECT * FROM `AHLTA$` WHERE `[First Name],[Rank],[Last Name]` IS NOT NULL"
With wdDoc.MailMerge
    .OpenDataSource _
    Name:="Student Log-in.xlsm", _
    Connection:="AHLTA.dotx", _
    SqlStatement:=sql
    .Destination = wdSendToNewDocument
    .Execute
End With
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I got this to work, the only problem is that I have to run the macro twice.. Not sure why that is...

Code:
Private Sub AHLTA_Certificates_Click()
Set appWd = CreateObject("Word.Application")
appWd.Visible = True
 
appWd.Documents.Open Filename:="C:\trial\AHLTA Certificate.docx"
    ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
    ActiveDocument.MailMerge.OpenDataSource Name:= _
        "C:\Documents and Settings\User.Name\Desktop\Student Log-in.xlsm", _
        ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
        AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
        WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
        Format:=wdOpenFormatAuto, Connection:= _
        "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\Documents and Settings\user.name\Desktop\Student Log-in.xlsm;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=3" _
        , SQLStatement:="SELECT * FROM `AHLTA$`", SQLStatement1:="", SubType:= _
        wdMergeSubTypeAccess
    ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
End Sub
 
Upvote 0
I keep recieving Run-Time error '462':

The remote server machine does not exist or is unavailable....


But the files are on the same computer. not involving server.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top