• Automaton error with Word (Access 2k Word 2k Win 2k)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Automaton error with Word (Access 2k Word 2k Win 2k)

    Author
    Topic
    #400530

    Hello All

    I’m having some real difficulties sorting out some automation code. I have a Word template with all the relevant bokkmarks, some code tht fills the book marks, asks the user for a file name and saves the file. The code then either emails the file as an attachment using Outlook automation, or prints the file off to provide a hard copy.

    My problem is with the exit handler, it just can not clean up the instances of Word I’ve created, run time error 462 is what it says. Now I’ve been to the MSKB for a solution, I’ve read Wendell’s rather good tips on Automation, I’ve tried everything I understand from these and frankly I’m in over my head.

    If necessary I’ll post the code, but it’s long as the whoe thing runs from one button and uses input forms and message boxes to select the next activity.

    Assuming I’ve not got any .Active.Document type entries (the cause of error 462as far as I can understand it) what else do I need to ensure is covered by the objWord instance I use to create a word application? I’ve tried using the New Word.Application and the CreateObject(“Word.Application”) alternative to it (although I don’t see the difference, all I get is it’s a ‘binding’ issue).

    Apologies for the length of this post, but I’m trying to explain it without having anyone read through many many lines of code.

    Thanks

    Ian

    Viewing 1 reply thread
    Author
    Replies
    • #781988

      I would check and check again that you don’t have code that implicitly refers to Word instead of explicitly through the objWord object, it is all too easy to miss the culprit. If you are absolutely sure, there is not much we can say without seeing the code.

      • #782335

        Hans

        I’ve been going over this for a while now and the only lines I see that concern me are the ones where I’m filling the bookmarks. Heres a short sample: –

        With objWord.ActiveDocument.Bookmarks
        .Item(“DateRejected”).Range.Text = Me.DateReceived
        End With

        I’ve since seen examples with this coded differrently, for example: –
        WordDoc.BookMarks(“DataReference”).Select
        objWord.Selection.TypeText DataSource.Fields(“DataFieldName”).

        So, should I be using the objWord variable before the .Item line??

        If this isn’t the problem I’ll put the code up with any lines that carry out identical instructions removed.

        Thanks

        Ian

        • #782339

          No, the With statement specifies that .Item belongs to the objWord.ActiveDocument.Bookmarks object, so it is already dependent on objWord. In fact, objWord.Item would cause an error.

          • #782352

            Edited by HansV to move very long code fragment to text attachment (with the added advantage of preserving indentation)

            Drat, I was hoping I’d worked it out on my own…. Ah well.

            The code is inserted below, with a few lines taken out. The error handler and exit handlers are a mess as I’ve been trying all sorts of options, but don’t delete the previous untill I know it’s no good.

            Some variables are rem’d out due to other things I’ve got to add to this once the code works. I’m looking into writing separate modules, but the code wouldn’t be used anywhere else, so other than reducing the number of lines behind my form I don’t see an advantage.

            I’m actually quite impressed with this as it’s my first ever attempt at using Automation and the Outlook bit works…….. But the Word automation has got me by the wotsits….

            Thanks, and apologies for using you as a ‘syntax’ checker…..

            Ian

            • #782372

              OK, I’ve been doing some more work on this and I’ve noticed the problem is only happening when the user takes the print option. I should have noticed this last night, but it was the end of a long day……

              So, as the exit handler works for two out of three scenarios I’m guessing it’s the print code that’s wrong……. Should I be saying anything more specific in there? I’m printing to a network printer, as will all users, so there is some time where the thing is spooling in the print manager, but the form is available to use.

              Ian

            • #782379

              Try replacing objWord.ActiveDocument.PrintOut by objWord.ActiveDocument.PrintOut Background:=False (or WordDoc.PrintOut Background:=False.) This should make the macro wait until the printing has been done.

              By the way, here are some comments on your code:

              • I would first ask the user if he/she wants to mail or print the document. In the latter case, there is no need to save the document, and to start Outlook etc. Starting Outlook causes a lot of overhead, you should avoid it if you’re not going to use it.
              • You declare and assign variables strRecipientFax ans strRecipientName but never use them.
              • I don’t understand the way you set the e-mail recipients. You seem to have two identical recordsets, then you loop through one of them first to concatenate names into strRecipient, then loop through the other one and repeatedly add the same strRecipient to the recipients of the e-mail. The second loop does not refer to any field of the recordset. I fail to see the purpose of that.
              • You don’t close the recordsets, and set only one of them to Nothing at the end.
              • To be on the safe side, I would close the Word doc without saving it – if printing has updated fields, Word would prompt the user to save changes.

                Worddoc.Close SaveChanges:=wdDoNotSaveChanges

              • The code contains what might be your real e-mail address.
                [/list]
            • #782480

              Hans

              Thanks for the help.

              The two variabbles I’ve declared, strRecipientFAX and strRecipientNames are to be used by the code. The names on is goin into the body text marked For the Attention Of strRecipientNames. The FAX one will be offerred to the user if the email string is empty when tested. This is not written that yet as I’ve been hung up on sorting out the errant Word sessions.

              Thanks for pointing out I’d missed those recordsets, they had been overlooked.

              I have to offer them the opttion of saving, the usrs want to have the file, I’ve expalined about databases but due to things like local custom, traceability and habit they insist they want to save the file and fill up a bit more disk space…..

              Thanks for the help.

              email addy changed, I’d overlooked that as well.

              Ian

            • #782481

              Hans

              Thanks for the help.

              The two variabbles I’ve declared, strRecipientFAX and strRecipientNames are to be used by the code. The names on is goin into the body text marked For the Attention Of strRecipientNames. The FAX one will be offerred to the user if the email string is empty when tested. This is not written that yet as I’ve been hung up on sorting out the errant Word sessions.

              Thanks for pointing out I’d missed those recordsets, they had been overlooked.

              I have to offer them the opttion of saving, the usrs want to have the file, I’ve expalined about databases but due to things like local custom, traceability and habit they insist they want to save the file and fill up a bit more disk space…..

              Thanks for the help.

              email addy changed, I’d overlooked that as well.

              Ian

            • #782380

              Try replacing objWord.ActiveDocument.PrintOut by objWord.ActiveDocument.PrintOut Background:=False (or WordDoc.PrintOut Background:=False.) This should make the macro wait until the printing has been done.

              By the way, here are some comments on your code:

              • I would first ask the user if he/she wants to mail or print the document. In the latter case, there is no need to save the document, and to start Outlook etc. Starting Outlook causes a lot of overhead, you should avoid it if you’re not going to use it.
              • You declare and assign variables strRecipientFax ans strRecipientName but never use them.
              • I don’t understand the way you set the e-mail recipients. You seem to have two identical recordsets, then you loop through one of them first to concatenate names into strRecipient, then loop through the other one and repeatedly add the same strRecipient to the recipients of the e-mail. The second loop does not refer to any field of the recordset. I fail to see the purpose of that.
              • You don’t close the recordsets, and set only one of them to Nothing at the end.
              • To be on the safe side, I would close the Word doc without saving it – if printing has updated fields, Word would prompt the user to save changes.

                Worddoc.Close SaveChanges:=wdDoNotSaveChanges

              • The code contains what might be your real e-mail address.
                [/list]
            • #782373

              OK, I’ve been doing some more work on this and I’ve noticed the problem is only happening when the user takes the print option. I should have noticed this last night, but it was the end of a long day……

              So, as the exit handler works for two out of three scenarios I’m guessing it’s the print code that’s wrong……. Should I be saying anything more specific in there? I’m printing to a network printer, as will all users, so there is some time where the thing is spooling in the print manager, but the form is available to use.

              Ian

          • #782353

            Edited by HansV to move very long code fragment to text attachment (with the added advantage of preserving indentation)

            Drat, I was hoping I’d worked it out on my own…. Ah well.

            The code is inserted below, with a few lines taken out. The error handler and exit handlers are a mess as I’ve been trying all sorts of options, but don’t delete the previous untill I know it’s no good.

            Some variables are rem’d out due to other things I’ve got to add to this once the code works. I’m looking into writing separate modules, but the code wouldn’t be used anywhere else, so other than reducing the number of lines behind my form I don’t see an advantage.

            I’m actually quite impressed with this as it’s my first ever attempt at using Automation and the Outlook bit works…….. But the Word automation has got me by the wotsits….

            Thanks, and apologies for using you as a ‘syntax’ checker…..

            Ian

        • #782340

          No, the With statement specifies that .Item belongs to the objWord.ActiveDocument.Bookmarks object, so it is already dependent on objWord. In fact, objWord.Item would cause an error.

      • #782336

        Hans

        I’ve been going over this for a while now and the only lines I see that concern me are the ones where I’m filling the bookmarks. Heres a short sample: –

        With objWord.ActiveDocument.Bookmarks
        .Item(“DateRejected”).Range.Text = Me.DateReceived
        End With

        I’ve since seen examples with this coded differrently, for example: –
        WordDoc.BookMarks(“DataReference”).Select
        objWord.Selection.TypeText DataSource.Fields(“DataFieldName”).

        So, should I be using the objWord variable before the .Item line??

        If this isn’t the problem I’ll put the code up with any lines that carry out identical instructions removed.

        Thanks

        Ian

    • #781989

      I would check and check again that you don’t have code that implicitly refers to Word instead of explicitly through the objWord object, it is all too easy to miss the culprit. If you are absolutely sure, there is not much we can say without seeing the code.

    Viewing 1 reply thread
    Reply To: Reply #782335 in Automaton error with Word (Access 2k Word 2k Win 2k)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel