Remove MS Word macro using VBScript

筅森魡賤 提交于 2020-01-05 09:07:30

问题


I want to remove all vba-modules from an MS Word template using VBScript. I 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


回答1:


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 


来源:https://stackoverflow.com/questions/2342526/remove-ms-word-macro-using-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!