Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Word macro from VBscript - If it exists

Status
Not open for further replies.

goldyr

Programmer
May 26, 2003
25
SE
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:
WordApp.Run("ProtectDocument")
 
i cant answer your question exactly i am afraid but you could try...

wrap the

WordApp.Run("ProtectDocument")

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
 
Yes this works. Thanks!

On Error Resume Next
WordApp.Run("ProtectDocument")
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.
 
fair point, could you use naming convention for the .dot file _p in the name for ones you want to protect? silly idea maybe.
 
Yes maybe, can you help me with the code?

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

How do I write it in VBCScript??
The document name is "Kvalitet Bedömning prod.dot
 
WordApp.Documents("Kvalitet Bedömning prod.dot").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
 
>[tt]ActiveDocument.Protect Password:="",NoReset:=False,Type:= _
wdAllowOnlyFormFields[/tt]
[tt]ActiveDocument.Protect 2,False,""[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top