Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Lounger
    Join Date
    Aug 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word VBA / Macro problem with print & browser (Word 2000)

    We are developing a Java application and using VB and VBA code to perform a mail merge and then print. We are successfully using a JSP to run a VB module which performs a mail merge. We are trying to use VBA code to automatically print the mail merge doc while it is embedded in a browser window. We have macro code which will print the document, but it will not shut down Word or the IE window after it is printed. It appears to me that once the Word VBA code to print is executed, the document is no longer recognized as a Word doc, therefore any Word VBA code set to run after the print statement will not run. I know this is not the best print solution, but it was what I have to work with. Here is the code which generates a "4237 runtime error, command is not available" after ActiveDocument.PrintOut when it tries to run ActiveWindow.Close False:

    Sub printclose()
    Application.Visible = False
    ActiveDocument.PrintOut
    ActiveWindow.Close False
    ActiveDocument.Close False
    Application.OnTime _
    when:=Now + TimeValue("00:0:15"), _
    Name:="wantquit"
    End Sub

    Any ideas? I thought maybe I could work with the InternetExplorer.Application object, but I need code to recognize the existing object, can't get it to work with CreateObject.

    Thanks,
    Chris

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    What happens if you take out ActiveWindow.Close False and just leave ActiveDocument.Close False ?

  3. #3
    New Lounger
    Join Date
    Aug 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    Tried that. Same error. Doesn't recognize ActiveDocument.Close False either.

    Thanks,
    Chris

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    Do you have a variable pointing to the Word Application object so that you can try a syntax like

    wrdApp.ActiveDocument.Close False

    StuartR

  5. #5
    New Lounger
    Join Date
    Aug 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    I tried that, but I could not figure out how to point to the existing instance of Word. I don't want to create another instance of Word and all I could figure out how to do was set the variable to a new object through code like:

    Set objWord = New Word.Application

    Is there a way to set the Word Application object without creating a new instance?

    Thanks,
    Chris

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    Set objWord = GetObject(, "Word.Application")

    will return a reference to a running instance of Word. It errors if Word is not running.

  7. #7
    New Lounger
    Join Date
    Aug 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    Just tried GetObject and now get a 4605 error, "this property or method is not available because this doc is in another app" at the same point of code execution, as it tries to execute MyWord.ActiveDocument.Close False.

    Thanks,
    Chris

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    Maybe you should post the complete code you are now using (at least, the part dealing with Word).

  9. #9
    New Lounger
    Join Date
    Aug 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    Here it is:

    Sub AutoOpen()
    Dim MyWord As Object
    Set MyWord = GetObject(, "Word.Application")
    MyWord.Visible = False
    MyWord.ActiveDocument.PrintOut
    MyWord.ActiveDocument.Close False
    Application.OnTime _
    when:=Now + TimeValue("00:0:15"), _
    Name:="wantquit"
    End Sub

    Sub wantquit()
    MyWord.Quit False
    End Sub

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    I can see several potential problems with this code:

    - It uses a running instance of Word; are you sure that Word will always be open when you run this?
    - It tries to quit Word - it is dangerous to do so if you started by assuming that Word was already running. The user might not like the rug being pulled from under him.
    - The setting for background printing might influence the result of trying to close the document immediately after printing it.
    - MyWord is used in WantQuit, but it is undefined there; MyWord is a local variable withing AutoOpen. You should declare it before all procedures/functions.

  11. #11
    New Lounger
    Join Date
    Aug 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    I can see several potential problems with this code:

    - It uses a running instance of Word; are you sure that Word will always be open when you run this?
    Yes, the VB code called by the JSP launches the Word doc that has the auto open macro..
    By opening the Word doc in the browser, Winword.exe is launched also
    - It tries to quit Word - it is dangerous to do so if you started by assuming that Word was already running. The user might not like the rug being pulled from under him.
    See answer to 1. The macro is in Word, so Word has to be running
    - The setting for background printing might influence the result of trying to close the document immediately after printing it.
    Have tried to comment out this line, object is still not recognized
    - MyWord is used in WantQuit, but it is undefined there; MyWord is a local variable withing AutoOpen. You should declare it before all procedures/functions.
    I tried defining it there, but something changes after the PrintOut statement so that Word is no longer recognized

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    I hope somebody else can help you, I don't know how applications behave in a web browser.

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    I'm trying to understand the context here. A JSP calls a VB program. I don't need to know how that happens. Or maybe I do, since I thought JSP was a server side technology. What exists in the web page(s) delivered to the browser? Is there some script that instantiates these objects? I guess I'm lost at the first step.

    Going on to the VB program, let's suppose that I run a VB program on my own computer. For maximum control, I would want to have a specific object reference to the Word application and to each document I expressly create. With a merge, does the merge method return a document object that you can latch on to rather than trying to use Activethis and Activethat?

    If you can't get a direct object reference to the merge document, you're going to have a difficult time calling native Word functions. However, you might be able to identify the extraneous window and send it a Windows "close" message without using any COM whatsoever. This might generate a nasty prompt for you to save the document. I'm not sure I have any solutions for that (other than third party products you probably don't want to load on the client).

    Can you sketch in some of the missing parts of the picture for me?

  14. #14
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    hi,

    i don't think we need Chris' setup to answer his question. i might be mistaken, but it looks to me, the vba code is called locally inside word. therefore, i don't think you even need to set a reference to the word application. also bringing the object variable referencing word on the module level will make it visible to the wantquit procedure, but it will never be called because the document containing the procedure is being closed before it can be called, leaving you with an orphaned word process.

    if i create a word doc with a module containing the following procedure:

    <pre>Sub AutoOpen()
    With ActiveDocument
    .PrintOut Background:=False
    .Parent.Quit
    End With
    End Sub
    </pre>



    and open this document directly inside internet explorer, it prints out and closes the word process as it should.

    good luck!

  15. #15
    New Lounger
    Join Date
    Aug 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word VBA / Macro problem with print & browser (Word 2000)

    I didn't write the JSP part or the VB part, but I believe Pieter has the concept. I don't think it matters who or what launches the Word doc in IE, because once the doc is opened locally, the VBA code is launched locally. In fact, I am testing just as Pieter did, by skipping the whole JSP, VB, mail merge process and just opening the Word doc in IE and the AutoOpen sub fires off.
    I copied Pieter's code into a module in a new doc, opened it in IE, it prints, then errors out with the 4605 message about this method or property not being available because this doc is in another app and debug mode shows that it errors on the .Parent.Quit line. I am using Word 2000 and IE 6.0. I get the same result from my pc hooked up to the LAN and printing to a network printer and when opening the doc on a stand alone laptop with a local printer.
    How can it work for you and not for me? I really thought you were on to something. I would post the code, but I am testing with your code word-for-word.

    Thanks,
    Chris

Page 1 of 2 12 LastLast

Posting Permissions

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