Results 1 to 4 of 4

Thread: Open as Object

  1. #1
    Join Date
    May 2013

    Unanswered: Open as Object

    I currently have a print button and Module to run a mail-merge through a query, of the current results on a form.

    When this is run, I get 3 documents open, the template, the merged document and another word document saying mail merge errors even though it works fine. At the beginning you also get a pop up box where you have to click options and check system tables. When the code runs, it creates an excel document from the query, for the record which is to be merged. Once this is done, simply double clicking the word document will ask yes or no to whether you want to merge, without all the unnecessary documents and options, as the merge comes from the excel.

    I've attempted to modify the code to just create the excel document and then open the word document, however, when VB is opening a word template it opens as object. When this happens it doesn't run the merge.

    Is there a way to open a word template through VB where it creates a new document and runs the merge, the same way as if I double clicked the template from the folder?

    Code currently used is:

    Private Sub Print_Click()
        DoCmd.OutputTo acOutputQuery, "RFQ Query", acFormatXLS, "H:\Request for Quotation\Merge.xls"
    Dim oApp         As Object
        Dim oDoc         As Object
        Dim sTmpltName      As String
        sTmpltName = "H:\Request for Quotation\"
        On Error Resume Next
        Set oApp = GetObject(, "Word.Application")
        If Err.Number <> 0 Then    'Word isn't running so start it
            Set oApp = CreateObject("Word.Application")
        End If
       On Error GoTo 0
       oApp.Visible = True
       Set oDoc = oApp.Documents.Add(sTmpltName)
        Set oDoc = Nothing
        Set oApp = Nothing
    End Sub
    Last edited by Diasflac; 07-25-13 at 07:53.

  2. #2
    Join Date
    Nov 2011
    To eliminate qry errors you could use DoCmd.SetWarnings False
    and then after the code, remember to set DoCmd.SetWarnings True

  3. #3
    Join Date
    May 2013
    There's nothing wrong with the query.

    When I run the above code it populates the excel file. But when it opens the word document, word doesn't even attempt to merge.

    If I close that document down, as the excel document is already populated, then manually open the template. Merge asks you if you want to run it and runs it giving the results.

    What I want is for the VBA to open the document the same way, so it actually runs the merge the same way as if you manually opened it.

    My thoughts are the openasobject command somehow stops the merge running, maybe interpretting it as an error?

    I tried your commands before and after, the same happens, it opens the document without merging and just displays the merge fields with no option to display mergedata.

  4. #4
    Join Date
    Jul 2013
    This is really a Word VBA issue.

    You should check out the following link:

    Mail Merge: Part I

    I believe you will find an answer to your issue by checking out the OpenDataSource method.

Posting Permissions

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