Results 1 to 4 of 4

Handling SPACE in [SQLstatement] argument during mailmerge

Threaded View

  1. #1
    Registered User
    Join Date
    07-31-2012
    Location
    Hong Kong
    MS-Off Ver
    Excel 2016
    Posts
    6

    Question Handling SPACE in [SQLstatement] argument during mailmerge

    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
    Last edited by Zlll; 11-06-2017 at 07:42 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Shift+Space and Ctrl+Space shortcuts intermittently working
    By yumyumdimsum in forum Excel General
    Replies: 8
    Last Post: 10-14-2017, 11:54 AM
  2. Using an IF argument & COUNTIF argument in the same cell formula
    By TommyK25 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-14-2013, 08:52 AM
  3. [SOLVED] ByRef argument type mismatch / Passing array as a function argument
    By pzling in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-29-2012, 06:23 PM
  4. [SOLVED] Handling imported dates, some with a trailing space
    By JonnieB in forum Excel General
    Replies: 5
    Last Post: 10-04-2012, 09:51 AM
  5. Replies: 3
    Last Post: 04-28-2010, 05:54 PM
  6. Function (array argument, range argument, string argument) vba
    By Witek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-24-2005, 11:07 AM
  7. Error handling with a handling routine
    By ben in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-15-2005, 11:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1