Hi, I have written mail merge code. however once the word document is open by the code I'm stuck at table option. attaching the image and code for your reference.
VBA Code:
Sub SOCMacro()
Dim d As String
Dim e As String
Dim strSql As String
a = Range("B1").Value
b = Range("B2").Value
c = Range("B3").Value
Application.DisplayAlerts = False
Application.ScreenUpdating = False
d = a & "\" & b
e = a & "\" & c
strSql = "SELECT * FROM 'RawData$'"
Call MergeRun(d, e, strSql)
End Sub
Sub MergeRun(frmFile As String, datfile As String, SQL As String, Optional bClose As Boolean = False, Optional bPrint As Boolean = False, _
Optional iNoCopies As Long = 1)
If Dir(frmFile) = "" Then
MsgBox "Form file does not exist." & vbLf & frmFile, vbCritical, "Exit - Missing Form File"
End If
If Dir(datfile) = "" Then
MsgBox "Data file does not exist." & vbLf & datfile, vbCritical, "Exit - Missing Data File"
End If
If Dir(frmFile) = "" Or Dir(datfile) = "" Then Exit Sub
Dim wdApp As New Word.Application, myDoc As Word.Document
With wdApp
.Visible = True
Set myDoc = .Documents.Open(frmFile, False, False, False)
.ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
.ActiveDocument.MailMerge.OpenDataSource Name:=datfile, _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=False, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:=SQL, SQLstatement1:="", _
SubType:=wdMergeSubTypeOther
With wdApp.ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
.DisplayAlerts = wdAlertsAll
If bPrint = True Then .ActiveDocument.PrintOut Copies:=iNoCopies
If bClose = True Then
.ActiveDocument.Close False
.Quit
End If
End With
Set myDoc = Nothing: Set wdApp = Nothing
End Sub