Reputation: 15703
I'm using a vbscript to programmatically run through a list of word documents, open each one, modify it, then save it using ms word 2010. My problem is there are protected and unprotected documents in the list. When I reach a protected document, I get this error: This method or property is not available because the object refers to a protected area of the document.
So I did some research and found this code on the ms website:
If objDoc.ProtectionType <> wdNoProtection Then
objDoc.Unprotect
End if
Now the problem is a new error: The unprotect method or property is not available because the document is already unprotected. Is there a another way to check if the document is protected or unprotected, when you know there will be both in the list, to avoid the errors?
Upvotes: 1
Views: 4828
Reputation: 38745
If you automate MS Office applications using VBScript (and use VBA sample code as a starting point), you may overlook the necessity of defining the wd*, xl*, or ad(?)* constants that are predefined in VBA, but missing in VBScript. Use the Docs and/or Debug output to add lines like:
Const wdNoProtection = <correct value>
to your script.
If you start your script with Option Explicit
and either avoid the evil global On Error Resume Next
entirely or at least disable it until the program is tested, you won't miss any of these beasts.
An even better approach is to write your script as a .wsf file. The <reference>
tag 'includes' the definitions (so you can't be blamed for an incorrect Const line).
POC/Demo code:
<?xml version="1.0" standalone="yes" encoding="iso-8859-1" ?>
<package>
<job id="QEC">
<reference object="Excel.Sheet" reference="true"/>
<script language="VBScript">
<![CDATA[
' ############################################################################
a = inputbox ( "Name of an Excel Constant?" )
msgbox a & " = " & eval(a)
' ############################################################################
]]>
</script>
</job>
</package>
Upvotes: 4