vba 使用 VBScript 删除 MS Word 宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2342526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove MS Word macro using VBScript
提问by NetForce1
I want to remove all vba-modules from an MS Word template using VBScript. I wrote the following script.
我想使用 VBScript 从 MS Word 模板中删除所有 vba 模块。我写了以下脚本。
const wdDoNotSaveChanges = 0
WScript.Echo "starting Word..."
Dim oApplication, doc
Set oApplication = CreateObject("Word.Application")
WScript.Echo "opening template..."
oApplication.Documents.Open "path\to\test.dot"
Set doc = oApplication.ActiveDocument
Dim comp, components
Set components = oApplication.ActiveDocument.VBProject.VBComponents
For Each comp In components
components.Remove comp
Next
WScript.Echo "exiting..."
doc.close wdDoNotSaveChanges
oApplication.Quit wdDoNotSaveChanges
When running similar code in a VBA-module in Word, that works, but when I run this VBScript, I get this error: test.vbs(14, 2) Microsoft VBScript runtime error: Invalid procedure call or argument
在 Word 中的 VBA 模块中运行类似的代码时,可以正常工作,但是当我运行此 VBScript 时,出现此错误: test.vbs(14, 2) Microsoft VBScript runtime error: Invalid procedure call or argument
采纳答案by NetForce1
It turns out that it is not possible to remove the VBComponent named "ThisDocument" (If you right click it in the IDE the remove option is not active). You can use something like:
事实证明,无法删除名为“ThisDocument”的 VBComponent(如果您在 IDE 中右键单击它,则删除选项未激活)。你可以使用类似的东西:
For Each comp In components
If comp.Name <> "ThisDocument" Then
components.Remove comp
End If
Next