使用VBScript删除MS Word宏

时间:2010-02-26 15:08:09

标签: vba vbscript ms-word word-vba

我想使用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

在Word中的VBA模块中运行类似代码时,可行,但是当我运行此VBScript时,我收到此错误:test.vbs(14, 2) Microsoft VBScript runtime error: Invalid procedure call or argument

1 个答案:

答案 0 :(得分:1)

事实证明,无法删除名为“ThisDocument”的VBComponent(如果在IDE中右键单击它,则删除选项不活动)。你可以使用类似的东西:

For Each comp In components 
    If comp.Name <> "ThisDocument" Then
        components.Remove comp
    End If
Next