Results 1 to 8 of 8
  1. #1
    stone1eye
    Guest

    VBA & Word MailMerge

    I currently have a vb application which produces a word mailmerge document based on some options buttons on the form.

    My problems is that once the user has selected some options buttons and produced the Word document, then if the user closes the Word document manually (ie. > File>Close), and then if they go back to the form to produce a new Word document I get an Automation Error.
    Any suggestions on why this error occurs would be much appreciated

    Tunc

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: VBA & Word MailMerge

    This is VB, right, not VBA, and you've built a form in VB that performs a Word mail merge? What versions of VB and Word are you using?

    How are you controlling the Word mail merge, DDE or activex/OLE automation? If you post the automation code, someone can help you with it, I'm sure.
    Charlotte

  3. #3
    stone1eye
    Guest

    Re: VBA & Word MailMerge

    sorry I wasn't all that clear the first time

    I currently have a vb form which performs a word mailmerge based on some options buttons on the form. Word mailmerge is controlled by activex/OLE automation (VB 5.0 & Word 97).

    The problem that I have encountered is that once the user has selected some options buttons and the mailmerge occurs, then if the user closes the Word document manually (ie. > File>Close), and then if they go back to the form (without reloading) to do another mailmerge I get an Automation Error.

    I have the following code in place to detect wheter there is a instance of word or not, and to do something accordingly.

    If Not WordObj Is Nothing Then
    If WordObj.Documents.Count > 0 Then
    ActiveDocument.Close wdDoNotSaveChanges
    WordObj.Quit
    End If
    Else
    If WordObj Is Nothing Then
    'do nothing
    End If
    End If

    Any suggestions as to why this error would be much appreciated.

    Tunc

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: VBA & Word MailMerge

    But the user is closing the Word document, right? So is WordObj Nothing at that point?

    How are you instantiating the object in the first place? If you instantiate the object when your form loads, then I'm assuming it gets broken when the user closes the document. You need to show us the code that instantiates the object and tell us where that happens. It's that code that has to be causing the error, not necessarily because it's wrong but possibly because you need additional code to re-instantiate the object if the document gets closed.
    Charlotte

  5. #5
    stone1eye
    Guest

    Re: VBA & Word MailMerge

    Sorry I was meant to say that if the user "Exits" the Word document (>File>Exit ) then the instance of the Word object and Word document becomes nothing.

    The object is initiated in a Public Function which gets called by a Public Precedure in which some validation occurs and the MailMerge is done. This Public procedure is then called by a command button. So everytime the command button is clicked (without reloading) the Public procedure is called.

    'In the following code the object instances have been
    'declared as public in the general decalrations section.

    'initiating object

    Public Function WordOpen() As Boolean
    Screen.MousePointer = vbHourglass
    On Error Resume Next
    '*********
    'see if word is running
    '*********
    Set WordObj = GetObject(, "word.Application")

    '*********
    'if it is not running, start new instance of word
    '*********
    If WordObj Is Nothing Then
    Set WordObj = CreateObject("word.Application")
    End If


    If WordObj Is Nothing Then
    MsgBox "Can't Create Word Object"
    WordOpen = False
    Else

    '*********
    'Make Word object visible
    '*********

    If Not WordObj.Visible Then
    WordObj.Visible = True
    End If

    WordOpen = True
    End If

    Screen.MousePointer = vbDefault
    End Function

    Thanks so much for the Help
    Tunc

  6. #6
    stone1eye
    Guest

    Re: VBA & Word MailMerge

    Just wondering wheter you had any suggestions with regards to the problem I was having?

    thanks

    Tunc

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: VBA & Word MailMerge

    Sorry, but I don't have any suggestions. Have you tried posting the question in the Word forum? You might have better luck there.
    Charlotte

  8. #8
    New Lounger
    Join Date
    Sep 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA & Word MailMerge

    It seems to me, that you're getting the error when the user closes WORD, not the document. Word is the automation server, and when it is closed by the user, the link breaks, and you get the Automation Error. It's important to note that the object reference IS NOT Nothing at this point...that's why you get the error.

    Here are a couple of things you can try to get around it...
    1) Dimension your wordObject WithEvents so that when the user closes Word, you can trap the Quit event in your app and at that point set your object reference to Nothing.
    2) Dimension a Document object WithEvents and work with IT rather than wordObject.Document. Here again, you can use the document's Close event to know when/if the user closes the document.

Posting Permissions

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