Below is my vba code for calling a mailmerged word doc with data filtred for any cell under the [Status] column that contains the value 'Pending report'.
These code resulted in infinite loop of error "Excel is waiting for another application to complete an OLE action".
However, if the value to be matched is changed to one that does NOT contain any SPACE e.g. WHERE [Status] = 'Done', everything's fine.
I attempted to handle the SPACE with the following ways but to no avail:
- 'Pending'&' '&'report'
- [Pending report]
- '"Pending report"'
- 'Pending%report'
- ""Pending report""
I have try googling the solution for the whole day Any help would be much appreciated!
Sub Gen_Report()
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
CurrentWorksheet = ActiveSheet.Name
excelPath = ThisWorkbook.Path & "\XYZ.xlsm"
Worksheets("New App").Visible = True
Sheets("New App").Select
wordPath = ThisWorkbook.Path & "\MailMerge.docx"
Set wordApp = CreateObject("Word.Application")
Set wordDoc = wordApp.Documents.Open(wordPath)
Set wordMailMerge = wordDoc.MailMerge
wordMailMerge.OpenDataSource Name:=excelPath, SQLStatement:="SELECT * FROM [New App$] WHERE [Status] = 'Pending report'"
wordMailMerge.Destination = wdSendToNewDocument
wordMailMerge.SuppressBlankLines = True
wordMailMerge.Execute Pause:=True
wordApp.Visible = True
wordDoc.Close savechanges:=False
End Sub
Bookmarks