Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


Run Word macro from VBscript - If it exists

Run Word macro from VBscript - If it exists

I have a VBScript that creates a new document from a teplate (.dot) and inserts data from another application. When the data has been inserted I want to run the macro that protects the document(allow only forms). It is working when the macro exists in the wordtemplate. If I want to open another template I get an error message because the macro does not exist. How can I make the script to run only if the macro exists in the word template? My code for running macro:

RE: Run Word macro from VBscript - If it exists

i cant answer your question exactly i am afraid but you could try...

wrap the


in an On Error Resume Next and catch the error?
not the same as a If WordApp.MacroExists(...) but might do what you want?

alternatively how about putting the code from "ProtectDocument" into your script so you dont have to worry about what the .dot file contains. might make maintence easier?

you will prob find the VBA forum might get you better results.
good luck

RE: Run Word macro from VBscript - If it exists

Yes this works. Thanks!

On Error Resume Next
   On Error Goto 0

If I put the code in the script I still have the problem with other documents that I don't want to protect. i us the same script for all documents.

RE: Run Word macro from VBscript - If it exists

fair point, could you use naming convention for the .dot file _p in the name for ones you want to protect? silly idea maybe.

RE: Run Word macro from VBscript - If it exists

Yes maybe, can you help me with the code?

The macro says:
ActiveDocument.Protect Password:="",NoReset:=False,Type:= _

How do I write it in VBCScript??
The document name is "Kvalitet Bedömning"

RE: Run Word macro from VBscript - If it exists

WordApp.Documents("Kvalitet Bedömning").Protect 2, False, "" ' 2=wdAllowOnlyFormFields

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886

RE: Run Word macro from VBscript - If it exists

>ActiveDocument.Protect Password:="",NoReset:=False,Type:= _

ActiveDocument.Protect 2,False,""

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close