Mail Merge Macro Doesn't Connect Refresh

StephenBart

New Member
Joined
Jun 22, 2023
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I have built a excel loader to prepare estimates and populate customer proposals from estimates in a macro enabled excel workbook. The macro opens a mail merged word document from a path and filename based on the contents of cell values - and then creates a pdf of a section of the word document - which works fine.

But I need it to update or refesh the data in the mail merge data before it exports the pdf. Because it does not do this I have to manually perform the mail merge in word (i.e. Mailing -> Select Existing Recipients -> Use an Existing List -> then open the workbook (the same workbook I just launched the macro from) and select the table named 'a_Proposal_Loader$' and print the now refreshed section 4 as pdf.

Is there a way to automate the mail merge and refresh the data upon opening the word doc?

Can anyone help?

Here is my code

VBA Code:
Sub PublishWalkthru()

    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim PDFsection As Word.Range
    Dim FileName As String
    Dim PDFfileName As String
    Dim Path As String
   
    Path = Range("D58").Value & "\"
    FileName = Range("D61").Value
    PDFfileName = Range("D62").Value
   
    'Set WordApp = CreateObject("Word.Application")
    Set WordApp = New Word.Application
   
    With WordApp
        .Visible = True
        .Activate
        Set WordDoc = .Documents.Open(FileName:=Path & FileName & ".docm")
    End With
   
    'Get Section 4 for exporting to PDF
   
    Set PDFsection = WordDoc.Sections(4).Range
   
    'If the last character in this section is a Section Break (Continuous or Next Page) (ASCII 12) then adjust the PDFsection range by 1 character to exclude it
   
    If Asc(PDFsection.Characters.Last) = 12 Then
        'PDFsection.Select  'the Selection includes the Section Break character
        Set PDFsection = WordDoc.Range(PDFsection.Start, PDFsection.End - 1)
        'PDFsection.Select   'the Selection now doesn't include the Section Break character
    End If
   
    PDFsection.ExportAsFixedFormat OutputFileName:=Path & PDFfileName & ".pdf", ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, Item:=wdExportDocumentContent

    PDFsection.Collapse wdCollapseStart
    PDFsection.Select
   
    WordApp.Windows(1).ScrollIntoView PDFsection, True

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Sorry - I should just mention that if I open the word document from (say) windows explorer it does pull the latest mail merge data from the table, but when the macro referenced above opens the same mail merged document it does not and I have to manual refresh each time?
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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