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