Open as Object

diasflac

Registered User.
Local time
Today, 04:45
Joined
May 13, 2013
Messages
45
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?
 
diasflac, Is it possible to show us some code? It is hard to get the idea what you are doing wrong without seeing any coding..
 
The code I'm using at the moment is:

Code:
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\Merge.dot"
    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

So the query writes to excel, then the word template is opened but runs no merge. If I double click into the same merge.dot file after this fails, because the excel document is populated it asks me if I want to run the merge, and does so displaying the results.

I think the only reason it's not asking to run the merge here is because it's running as object. Like if you double click the template, Access also opens a new document, the different is through access it doesn't ask you if you want to merge or not first.

I could be wrong, and happy to be shown as such :)
 

Users who are viewing this thread

Back
Top Bottom