vba - 如何像在 Microsoft Excel 中那样创建 Word VBA 个人 VBA 代码库?

标签 vba excel ms-word

如何像在 Microsoft Excel 中那样创建 Word VBA 个人代码库?我本来可以正常工作,但今天它坏了,我无法让它再次工作。我最初这样做是因为我的 Normal.dot 文件崩溃了并且我丢失了所有宏。我找到了一篇博客文章,描述了如何做到这一点,但我找不到返回那里的方法。我在 Google 上进行了广泛的搜索,但没有找到解决方案。

这里有一些曲折,如下所述......

在 Microsoft Excel 中,可以轻松创建“代码库项目”。这只是一个包含 VBA 模块的标准 XLSB 文件,与 ..\Excel\STARTUP 文件夹与 PERSONAL.XLSB 文件一起保存。

当 VBA 编辑器在任何其他 XLSX/B 文件中打开时,代码库文件中的模块始终可见。并且不需要创建 VBA 项目引用来查看它们。库模块可以进行 native 编辑,就好像它们是任何其他文件的组成部分一样,不需要在另一个窗口中打开库文件即可这样做,如图所示...

enter image description here

然而...在 Microsoft Word 中,它的工作方式似乎有所不同,而且...很奇怪。如果执行相同操作:使用自定义代码模块创建 DOCM 文件,并将其保存在 ..\Word\Startup\ 文件夹中,这些模块在 VBA 项目资源管理器中不可见.

沿着从 Excel 到 Word 的逻辑链,并按照其他人所做的那样...将 DOCM 保存为 DOTM,并将其保存在 ..\Templates 文件夹中,并使用Normal.dotm 文件。尽管如此,它在 Project Explorer 中仍然不可见。

直到打开 Normal.dotm 进行编辑并且将库 DOTM 添加为 VBA 项目引用后,它才最终出现在项目资源管理器中,但仅在 Normal 打开时才出现。另外...它会打开代码库文件以便在另一个窗口中进行编辑...!如果使用该引用保存 Normal,并从中创建新文档,则无法编辑库代码并显示错误“项目无法查看”...

enter image description here

直到库 DOTM 文件在第二窗口中打开进行编辑时,它的代码才变得可编辑,并且它同时在所有文档的所有 Word VBA 窗口中进行编辑。

有一线希望:如果我从库 DOTM 而不是 Normal.DOTM 创建一个新的空白 DOC,它就可以工作..!从库创建的任何文档都保留指向代码模块的链接,包括任何新的或编辑的文档,并且可以在项目资源管理器中对它们进行 native 编辑。这也包括 Normal.DOTM 中的代码模块。

似乎“可编辑性”是一种方式:从任何其他模板创建的文档都可以查看和编辑自己的代码以及 Normal.DOTM 中的代码,但反之则不然:从 Normal.DOTM 创建的文档只能查看他们自己的代码。

如何为所有文档建立双向连接,无论它们是从什么模板创建的..??

我一直在尝试解决这可能是安全/恶意软件问题的可能性,但我无法想出一个。如果“信任”设置中已启用宏,则恶意软件 99% 已经在控制计算机了。剩下的唯一一步是启用 VBIDE。

最佳答案

A very similar question on Microsoft Answers :

Is there a startup or template file in Words that allows one to store "personal macros", i.e. macros that one wants to have always available while editing documents (I am seeking the equivalent to the PERSONAL.XLSB file in Excel, where I can store macros that then are available in all sheets that I open).

给出的解决方案:

Word uses the default template Normal.dotm for this purpose. Macros stored in Normal.dotm will be available in all documents. Apart from that, Normal.dotm also serves as the default document template, and it also stores the user's formatted AutoCorrect entries.

Just like Personal.xlsb, Normal.dotm doesn't exist in a clean new installation, but it will be created when needed.

The location for Normal.dotm is

C:\Users\<username>\AppData\Roaming\Microsoft\Templates

AppData is a hidden folder, so you must display hidden files and folders in order to see it in Windows Explorer.

Similar questions (也在answers.microsoft.com 上)已提供相同类型的解决方案。

Macros that should be globally available can be stored in Normal.dotm or in a template located in the Startup folder for Word. See also:

还有:

In Word, personal settings (those that aren't stored in the Registry) are stored in the Normal template, Normal.dotm. When you change and save settings or create macros, they are automatically saved in Normal.dotm unless you choose to save them in another template. Normal.dotm is automatically loaded when you start Word, and new Blank Documents are based on Normal.dotm.

关于vba - 如何像在 Microsoft Excel 中那样创建 Word VBA 个人 VBA 代码库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51618205/

相关文章:

c# - 如何使用 vs 2010、c# 以编程方式在 Word 2007 文档中插入或编辑饼图?

ms-word - 是否可以在 Word 文档中存储与特定表格或单元格相关联的隐藏元数据信息?

vba - 复制/粘贴不是主条目行的数据

正则表达式匹配一些可能的字符串与可能的前导和/或尾随空格

excel - 设置范围不适用于内部的 Cells()

vba - 如果语句自动筛选符合条件

excel - Excel 2016 中的图表名称

Excel VBA 函数在完成前停止执行而没有错误消息

excel - 将多行文本单元格从 Word 表复制到 Excel 单元格

loops - Else If 是否为 FileExists()