Call Word document (with mergefields ready) and MailMerge it from Excel (all hosted on Sharepoint)

herman925

New Member
Joined
Apr 9, 2017
Messages
24
I have to setup a series of mail merged forms for my school to invite guest speakers to our classrooms. For that, I have setup a table called Guest_Speaker_MM.
I've already created all the merge fields and tested them using manual mail merge and they worked fine. However, when these files are shared via Sharepoint (One Drive), the mail merge always unlinks even when I made sure the filepaths are identical on all other computers.
In frustration I looked for VBA to help me point to the proper path when I press a button on Excel.
These are the things I have so far but I am unable to achieve anything since I'm just a beginner of VBA
Code:
Private Sub ReminderButton_Click()
Dim WordObject As Object
Dim DocSource As Object
Dim RealWorkbookPath As String

On Error Resume Next
Set WordObject = GetObject(, "Word.Application")
If wd Is Nothing Then
   Set WordObject = CreateObject("Word.Application")
End If
On Error GoTo 0

Set DocSource = WordObject.Documents.Open(ThisWorkbook.Path & "\1.2 - Guest Speaker\05 - Reminder of Collection & Payment needed for Invited Guest Speaker.docx")
RealWorkbookPath = ThisWorkbook.Path & "/" & ThisWorkbook.Name

DocSource.MailMerge.MainDocumentType = wdFormLetters

DocSource.MailMerge.OpenDataSource _
Name:=RealWorkbookPath, _
Connection:="Data Source=" & RealWorkbookPath & ";Mode=Read", _
SQLStatement:="SELECT * FROM 'Guest Speakers$' where Status = Signed"

Unload Me
End Sub

I need to achieve these functions:

  • When a button is pressed, automatically open the specified word document (it's in a sub-folder of the workbook's path)
  • Execute the MailMerge function, with the WorkSheet Name being Guest Speakers and the Table Name is Guest_Speaker_MM
  • Apply a filter, where Column Header Status = Reminder Needed (I have no idea how to execute SQL Commands here)



Please help me
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Getting something like this now, which is in similar structure i suppose, but not working with error 91: "Object Variable or With block variable not set"

Code:
Private Sub CommandButton1_Click()
    Dim WordApp As Word.Application
    Dim OpenDocument As Word.Document
    Dim wordMailMerge As Word.Mailmerge
    Dim wordMergeFields As Word.MailMergeDataFields
    Dim wordPath As String
    Dim excelPath As String
    
    WordApp.DisplayAlerts = wdAlertsNone
    
    CurrentWorksheet = ActiveSheet.Name
    excelPath = ThisWorkbook.FullName

                wordPath = ThisWorkbook.Path & "\1.2 - Guest Speaker\01 - Approved Guest Speaker Notification.docx"
                Set WordApp = CreateObject("Word.Application")
                Set OpenDocument = WordApp.Documents.Open(wordPath)
                Set wordMailMerge = OpenDocument.Mailmerge
                
                wordMailMerge.OpenDataSource Name:=excelPath, SQLStatement:="SELECT * FROM `'Guest Speakers$'` where `Status`= 'Reminder Needed'"
                wordMailMerge.Execute
                OpenDocument.Close
                WordApp.Visible = True
        
    Set wordMailMergeFields = Nothing
    Set wordMailMerge = Nothing
    Set OpenDocument = Nothing
    Set WordApp = Nothing
    WordApp.DisplayAlerts = wdAlertsAll
    
    Sheets(CurrentWorksheet).Select
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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