开发者

Remove MS Word macro using VBScript

I want to remove all vba-modules from an MS Word template using VBScript. I开发者_如何学JAVA wrote the following script.


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


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:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜