Can't get MailMerge to work through VBA

DetroitJimBurke

New Member
Joined
Nov 7, 2022
Messages
11
Office Version
  1. 2013
Platform
  1. Windows
I'm trying to populate a Word document with info from a spreadsheet. I created Merge fields in the Word doc, e.g. <<Patient_Name>>. The excel book I'm testing with has two tabs, one called Sheet1 that has a row with the field names and a row with the values, the other sheet just has a button I will be having the user click to run the process. I found some code from your site where they're basically doing the same thing I'm trying to do. They posted their code and said it was working for them, so I copied and pasted their code and modified it to fit what I'm doing. It works up until it gets to this line of code:

VBA Code:
  wordMailMerge.OpenDataSource Name:=excelPath, SQLStatement:="SELECT * FROM `'Sheet1$'`"

It locks up for about 2 minutes and then starts giving me a message 'Microsoft Excel is waiting for another application to complete an OLE action' every 10 seconds or so.

Here is the complete subroutine:

VBA Code:
Sub RunMailMerge()
   On Error GoTo HandleError

    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    Dim wordMailMerge As Word.MailMerge
    Dim wordMergeFields As Word.MailMergeFields
    Dim wordPath As String
    Dim excelPath As String
    Dim LtrFinalRow As Long
       
    CurrentWorksheet = ActiveSheet.Name
    excelPath = ThisWorkbook.Path & "\Create Word Doc.xlsm"
    ' excelPath = "C:\Users\jimib_000\Documents\word template data.xlsx"

        ' Worksheets("Ltr Data").Visible = True
        Sheets("Sheet1").Select
        LtrFinalRow = Cells(Rows.Count, 1).End(xlUp).Row
        
            If LtrFinalRow > 1 Then
                'Clear any extra, blank rows.
                Range("B" & LtrFinalRow + 1).Select
                Range(Selection, Selection.End(xlToRight)).Select
                Range(Selection, Selection.End(xlDown)).Select
                Selection.Delete Shift:=xlUp
                
                wordPath = ThisWorkbook.Path & "\test template.docx"
                Set wordApp = CreateObject("Word.Application")
                Set wordDoc = wordApp.Documents.Open(wordPath)
                Set wordMailMerge = wordDoc.MailMerge
                
                wordMailMerge.OpenDataSource Name:=excelPath, SQLStatement:="SELECT * FROM `'Sheet1$'`"
                wordMailMerge.Execute
                wordDoc.Close
                wordApp.Quit
            End If
        ' Worksheets("Ltr Data").Visible = False
        
    Set wordMailMergeFields = Nothing
    Set wordMailMerge = Nothing
    Set wordDoc = Nothing
    Set wordApp = Nothing
    Sheets(CurrentWorksheet).Select
    Exit Sub
    
HandleError:
   MsgBox "An error has occurred."
   wordDoc.Close
   wordApp.Quit
   Set wordDoc = Nothing
   Set wordApp = Nothing
 
End Sub

Here's the data from the Sheet1 tab:

Patient_NameFirst_NameHis/HerCapsHis/HerHe/SheCapsHe/She
Jim BurkeJimHishisHehe

I'm not sure how to attach copies of the actual excel file and word doc, or if you need those. Here's the content of the Wotd doc, it's a shorter version of what I'll end up using. You can see the Merge fields, defiend with the <<>> delimiters.

Confidential Assessment Summary



Name: «Patient_Name»Examiners:
Age:
Date of BirthDates of Evaluation:
Date of Feedback


Reason for Referral:


«Patient_Name», a 6-year-old male, was referred by «His/Her» to determine the sources of current difficulties.



The current evaluation consisted of a diagnostic interview, direct testing, and observations of My_First_Name, and developmental and behavioral rating scales completed by My_His_Her_Small My_Guardian.



Background information was also obtained through an interview .



This is some text that I want to insert into a Word document. I'm going to use the function that word has of inserting from a file by inserting and choosing this file. Let's see what happens.

This is some text that I want to insert into using the AutoText function. I’m going to store this in auto text and then insert it and we’ll see what happens.



Johnathon Lebo, a 6-year-old male, was referred by his/her pediatrician at Kids First to determine the sources of current difficulties.



There are concerns with both sleep onset and sleep maintenance and since «He/She» was around 3-4 years of age. He is very overactive and has difficulty settling down at night. He often does not fall asleep until around midnight, then wakes up early in the morning at about 5-6 am. As a result, he is often fatigued during the day. His teachers have expressed concerns about his trying to sleep during class at times
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,223,879
Messages
6,175,142
Members
452,615
Latest member
bogeys2birdies

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