使用 VBScript 删除 MS Word 宏 [英] Remove MS Word macro using VBScript

查看:62
本文介绍了使用 VBScript 删除 MS Word 宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 VBScript 从 MS Word 模板中删除所有 vba 模块.我写了以下脚本.

<预><代码>const wdDoNotSaveChanges = 0WScript.Echo "开始 Word..."Dim oApplication,文档Set oApplication = CreateObject("Word.Application")WScript.Echo打开模板..."oApplication.Documents.Open "path\to\test.dot"设置 doc = oApplication.ActiveDocument调光组件、组件设置组件 = oApplication.ActiveDocument.VBProject.VBComponents对于每个 comp In 组件components.Remove comp下一个WScript.Echo退出..."doc.close wdDoNotSaveChangesoApplication.Quit wdDoNotSaveChanges

在 Word 中的 VBA 模块中运行类似的代码时,这可行,但是当我运行此 VBScript 时,出现此错误:test.vbs(14, 2) Microsoft VBScript 运行时错误:过程调用无效或参数

解决方案

事实证明,无法删除名为ThisDocument"的 VBComponent(如果您在 IDE 中右键单击它,删除选项将不可用).您可以使用以下内容:

For Each comp In 组件如果 comp.Name <>这个文件"然后components.Remove comp万一下一个

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

解决方案

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 

这篇关于使用 VBScript 删除 MS Word 宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆