Mail merge function with selected data

nareshjoshy

New Member
Joined
Mar 6, 2019
Messages
23
Dear Sir,

I have copied some code from your site. And now I want to change little bit thing in code.

Code:
Sub DoMailMerge()
'Nodte: A VBA Reference to the Word Object Model is required, via Tools|References
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim strWorkbookName As String: strWorkbookName = ThisWorkbook.FullName
With wdApp
  'Disable alerts to prevent an SQL prompt
  .DisplayAlerts = wdAlertsNone
  'Open the mailmerge main document
  Set wdDoc = .Documents.Open("C:\Users\DEO\Desktop\guarantee\123.doc", _
    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:="SELECT * FROM `Sheet2$`", _
        SubType:=wdMergeSubTypeAccess
      With .DataSource
        .FirstRecord = wdDefaultFirstRecord
        .LastRecord = wdDefaultLastRecord
      End With
      'Excecute the merge
      .Execute
      'Disconnect from the data source
      .MainDocumentType = wdNotAMergeDocument
    End With
    'Close the mailmerge main document
    .Close False
  End With
  'Restore the Word alerts
  .DisplayAlerts = wdAlertsAll
  'Display Word and the document
  .Visible = True
End With
End Sub
In this code I want to change two things.

1.
"C:\Users\DEO\Desktop\guarantee\123.doc"
I want to get this value from cell. i.e. I want to get this value from CellB28 of sheet2.
2. I don't want to merge all the data of sheet. I want to get option to enter from start record to end record. i.e. if I want to merge 2-5 record out of 1-10 record. each time I execute my function option should popup.

thank You in advance
 
Your own code shows how to limit the range of records to process...

All you need do is change:
VBA Code:
Dim i As Long, j As Long
to, for example:
VBA Code:
Dim i As Long, j As Long, x As Long, y As Long
and change:
VBA Code:
    For i = 1 To .DataSource.RecordCount
to:
VBA Code:
      x = CLng(InputBox("Enter the First Record # to merge", , 1))
      y = CLng(InputBox("Enter the Last Record # to merge", , .DataSource.RecordCount))
      If y > .DataSource.RecordCount Then y = .DataSource.RecordCount
    For i = x To y
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Your own code shows how to limit the range of records to process...

All you need do is change:
VBA Code:
Dim i As Long, j As Long
to, for example:
VBA Code:
Dim i As Long, j As Long, x As Long, y As Long
and change:
VBA Code:
    For i = 1 To .DataSource.RecordCount
to:
VBA Code:
      x = CLng(InputBox("Enter the First Record # to merge", , 1))
      y = CLng(InputBox("Enter the Last Record # to merge", , .DataSource.RecordCount))
      If y > .DataSource.RecordCount Then y = .DataSource.RecordCount
    For i = x To y
Thanks macropod. =)
 
Upvote 0

Forum statistics

Threads
1,223,320
Messages
6,171,432
Members
452,402
Latest member
siduslevis

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