Word Macro Runtime Error

Hi all,

I'm a complete newbie when it comes to VBA but I've been tasked with writing a macro for a Word form. The form will be linked to on a web page and has two buttons at the bottom, Accept or Decline.

Basically what the boss wants to happen is that when either button is clicked the form gets attached to an email and automatically sent. At the same time a message box appears telling the applicant "thanks" and after they click "OK" on the message box he wants EVERYTHING to close.

My problem is that 99% of the time the person is going to open the Word doc in IE and ActiveDocument doesn't seem to work outside of Word. When I run it in Word, no problem. When I test it in IE I get a runtime error because I can't apply the ActiveDocument.Close to an application outside of Word.

Any suggestions on what I can use instead to get IE to close the window

Here's the code:

Private Sub CommandButton1_Click()
ActiveDocument.HasRoutingSlip = True
With ActiveDocument.RoutingSlip
.Subject = "Form Yes"
.AddRecipient "email address here"
.Delivery = wdAllAtOnce
End With
ActiveDocument.Route
MsgBox "Thank you. Your form has been submitted. You will soon blah blah blah", vbOKOnly, "Message Title"
ActiveDocument.Close (SaveChanges = DoNotSaveChanges)
End Sub

Thanks for the assist!

Adrian



Answer this question

Word Macro Runtime Error

  • Mike Q

    Thanks for the reply, Derek.

    While it does close the window it first asks is I want to save the changes to the doc. If I hit "yes" or "cancel" the window stays open.

    Any way to bypass that

    A


  • newyuppie

    Oops,

    Never mind.

    I actually figured that one out.

    I put:

    ActiveDocument.Saved = True

    after

    SendKeys("%{F4}")

    and everything seems to work fine in my testing environment.

    Thanks again for your help.

    A


  • David d

    Hi,

    You can use the SendKeys method to close internet explorer if you pass in ALT-F4, the keyboard shortcut to close the current open window, it will close IE although there appears to be a slight delay before the instance is destroyed.

    Replace

    ActiveDocument.Close (SaveChanges = DoNotSaveChanges

    With

    SendKeys("%{F4}")



  • Word Macro Runtime Error