Option Explicit
Sub MergeToLetterNoPrompt()
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim i As Long, z As Long, lRec As Long
Dim strWorkbookName As String: strWorkbookName = ThisWorkbook.FullName
Const strQry As String = "SELECT * FROM `Data$` WHERE `REMARK L` LIKE '%Send%'"
lRec = ThisWorkbook.Sheets("Data").UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row - 1
Application.ScreenUpdating = False
With wdApp
'Disable alerts to prevent an SQL prompt
.DisplayAlerts = wdAlertsNone
'Open the mail merge main document
Set wdDoc = .Documents.Open(ThisWorkbook.Path & "\INPUT\ABC COMPANY.docx", _
ConfirmConversions:=False, ReadOnly:=True, AddToRecentFiles:=False)
With wdDoc
With .MailMerge
'Define the mailmerge type
.MainDocumentType = wdFormLetters
'Define the output
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
'Connect to the data source
.OpenDataSource Name:=strWorkbookName, ReadOnly:=True, LinkToSource:=False, _
AddToRecentFiles:=False, Format:=wdOpenFormatAuto, _
Connection:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
"User ID=Admin;Data Source=strWorkbookName;" & _
"Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:=strQry, SubType:=wdMergeSubTypeAccess
With .DataSource
.FirstRecord = 1
.ActiveRecord = 1
.LastRecord = lRec
End With
.Execute Pause:=False
'Disconnect from the data source
.MainDocumentType = wdNotAMergeDocument
End With
'Close the mailmerge main document
.Close False
End With
With .ActiveDocument
.SaveAs Filename:=ThisWorkbook.Path & "\OUTPUT\ABC COMPANY RECEIPT" & Format(Date, "dd mmmm yyyy"), _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
'Close the mailmerge output document
.Close False
End With
'Restore the Word alerts
.DisplayAlerts = wdAlertsAll
'Exit Word
.Quit
End With
'Mark sent 'Send' records 'Sent'
With ThisWorkbook.Sheets("Data")
For i = 2 To lRec + 1
If .Range("W" & i).Value = "Send" Then
If .Range("H" & i).Value <> "" Then
If InStr(.Range("H" & i).Value, "-") = 0 Then
.Range("W" & i).Value = "Sent"
z = z + 1
End If
End If
End If
Next
End With
Application.ScreenUpdating = True
Set wdDoc = Nothing: Set wdApp = Nothing
MsgBox "Congratulations! " & z & " letters were generated successfully!", 64
End Sub
Sub MergeToLetterWithPrompt()
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim i As Long, x As Long, y As Long, z As Long, lRec As Long
Dim strWorkbookName As String: strWorkbookName = ThisWorkbook.FullName
Const strQry As String = "SELECT * FROM `Data$` WHERE `REMARK L` LIKE '%Send%'"
lRec = ThisWorkbook.Sheets("Data").UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row - 1
'Get the record range to merge
x = InputBox("What is the first record to merge?", , 1)
y = InputBox("What is the last record to merge?", , lRec)
If x < 1 Or y < 1 Or y < x Then Exit Sub
'If the 'Y' is greater than last record then 'y' should be the last record
If y > lRec Then y = lRec
Application.ScreenUpdating = False
With wdApp
'Disable alerts to prevent an SQL prompt
.DisplayAlerts = wdAlertsNone
'Open the mail merge main document
Set wdDoc = .Documents.Open(ThisWorkbook.Path & "\INPUT\ABC COMPANY.docx", _
ConfirmConversions:=False, ReadOnly:=True, AddToRecentFiles:=False)
With wdDoc
With .MailMerge
'Define the mailmerge type
.MainDocumentType = wdFormLetters
'Define the output
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
'Connect to the data source
.OpenDataSource Name:=strWorkbookName, ReadOnly:=True, LinkToSource:=False, _
AddToRecentFiles:=False, Format:=wdOpenFormatAuto, _
Connection:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
"User ID=Admin;Data Source=strWorkbookName;" & _
"Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:=strQry, SubType:=wdMergeSubTypeAccess
With .DataSource
.FirstRecord = x
.ActiveRecord = x
.LastRecord = y
End With
.Execute Pause:=False
'Disconnect from the data source
.MainDocumentType = wdNotAMergeDocument
End With
'Close the mailmerge main document
.Close False
End With
With .ActiveDocument
.SaveAs Filename:=ThisWorkbook.Path & "\OUTPUT\ABC COMPANY RECEIPT" & Format(Date, "dd mmmm yyyy"), _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
'Close the mailmerge output document
.Close False
End With
'Restore the Word alerts
.DisplayAlerts = wdAlertsAll
'Exit Word
.Quit
End With
'Mark sent 'Send' records 'Sent'
With ThisWorkbook.Sheets("Data")
For i = x + 1 To y + 1
If .Range("W" & i).Value = "Send" Then
If .Range("H" & i).Value <> "" Then
If InStr(.Range("H" & i).Value, "-") = 0 Then
.Range("V" & i).Value = "Sent"
z = z + 1
End If
End If
End If
Next
End With
Application.ScreenUpdating = True
Set wdDoc = Nothing: Set wdApp = Nothing
MsgBox "Congratulations! " & z & " letters were generated successfully!", 64
End Sub
Thank you and have a nice time.
Bookmarks