简体   繁体   English

如何以编程方式检查word文档vbscript上的保护

[英]how to programmatically check protection on a word document vbscript

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. 我正在使用vbscript以编程方式运行word文档列表,打开每个文档,修改它,然后使用ms word 2010保存它。我的问题是列表中有受保护和未受保护的文档。 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: 所以我做了一些研究,并在ms网站上找到了这段代码:

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. 现在问题是一个新错误:unprotect方法或属性不可用,因为文档已经不受保护。 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? 有没有另一种方法可以检查文档是受保护还是不受保护,当您知道列表中有两个文件时,是否可以避免错误?

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. 如果使用VBScript自动化MS Office应用程序(并使用VBA示例代码作为起点),您可能会忽略定义在VBA中预定义但在VBA中预定义的wd *,xl *或ad(?)*常量的必要性。 VBScript中。 Use the Docs and/or Debug output to add lines like: 使用Docs和/或Debug输出添加如下行:

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. 如果您使用Option Explicit启动脚本并且完全避免邪恶的全局On Error Resume Next或者至少禁用它直到程序被测试,您将不会错过任何这些野兽。

An even better approach is to write your script as a .wsf file. 更好的方法是将脚本编写为.wsf文件。 The <reference> tag 'includes' the definitions (so you can't be blamed for an incorrect Const line). <reference>标签'包含'定义(因此不能因错误的Const行而受到指责)。

POC/Demo code: POC /演示代码:

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM