Mailmerge to Excel - Stuck once the vba code opens word document

siddo

Board Regular
Joined
May 26, 2020
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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
 

Attachments

  • Capture.PNG
    Capture.PNG
    20.8 KB · Views: 12

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
A reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,216,008
Messages
6,128,256
Members
449,435
Latest member
Jahmia0616

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