以编程方式从 Word 2007 文档中提取宏 (VBA) 代码
是否可以使用 API 从 Word 2007“docm”文档中提取所有 VBA 代码?
我已经找到了如何在运行时插入 VBA 代码,以及如何删除所有 VBA 代码,但不将实际代码提取到我可以存储的流或字符串中(并在将来插入到其他文档中)。
任何提示或资源将不胜感激。
编辑:感谢大家,Aardvark的答案正是我正在寻找的。 我已将他的代码转换为 C#,并且能够使用 Visual Studio 2008 从类库中调用它。
using Microsoft.Office.Interop.Word;
using Microsoft.Vbe.Interop;
...
public List<string> GetMacrosFromDoc()
{
Document doc = GetWordDoc(@"C:\Temp\test.docm");
List<string> macros = new List<string>();
VBProject prj;
CodeModule code;
string composedFile;
prj = doc.VBProject;
foreach (VBComponent comp in prj.VBComponents)
{
code = comp.CodeModule;
// Put the name of the code module at the top
composedFile = comp.Name + Environment.NewLine;
// Loop through the (1-indexed) lines
for (int i = 0; i < code.CountOfLines; i++)
{
composedFile += code.get_Lines(i + 1, 1) + Environment.NewLine;
}
// Add the macro to the list
macros.Add(composedFile);
}
CloseDoc(doc);
return macros;
}
Is it possible to extract all of the VBA code from a Word 2007 "docm" document using the API?
I have found how to insert VBA code at runtime, and how to delete all VBA code, but not pull the actual code out into a stream or string that I can store (and insert into other documents in the future).
Any tips or resources would be appreciated.
Edit: thanks to everyone, Aardvark's answer was exactly what I was looking for. I have converted his code to C#, and was able to call it from a class library using Visual Studio 2008.
using Microsoft.Office.Interop.Word;
using Microsoft.Vbe.Interop;
...
public List<string> GetMacrosFromDoc()
{
Document doc = GetWordDoc(@"C:\Temp\test.docm");
List<string> macros = new List<string>();
VBProject prj;
CodeModule code;
string composedFile;
prj = doc.VBProject;
foreach (VBComponent comp in prj.VBComponents)
{
code = comp.CodeModule;
// Put the name of the code module at the top
composedFile = comp.Name + Environment.NewLine;
// Loop through the (1-indexed) lines
for (int i = 0; i < code.CountOfLines; i++)
{
composedFile += code.get_Lines(i + 1, 1) + Environment.NewLine;
}
// Add the macro to the list
macros.Add(composedFile);
}
CloseDoc(doc);
return macros;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将代码导出到文件,然后将它们读回。
我一直在使用下面的代码来帮助我将一些 Excel 宏置于源代码控制之下(使用 Subversion 和 TortoiseSVN)。 每当我打开 VBA 编辑器保存时,它基本上都会将所有代码导出到文本文件。 我将文本文件放入 subversion 中,以便我可以进行比较。 您应该能够改编/窃取其中的一些内容以在 Word 中工作。
CanAccessVBOM() 中的注册表检查对应于安全设置中的“信任对 Visual Basic 项目的访问”。
You could export the code to files and then read them back in.
I've been using the code below to help me keep some Excel macros under source control (using Subversion & TortoiseSVN). It basically exports all the code to text files any time I save with the VBA editor open. I put the text files in subversion so that I can do diffs. You should be able to adapt/steal some of this to work in Word.
The registry check in CanAccessVBOM() corresponds to the "Trust access to Visual Basic Project" in the security setting.
您必须添加对 Microsoft Visual Basic for Applications Extensibility 5.3(或您拥有的任何版本)的引用。 我的盒子上有 VBA SDK 等 - 所以这可能不完全是 Office 附带的。
此外,您还必须专门启用对 VBA 对象模型的访问 - 请参阅 Word 选项中的“信任中心”。 这是对 Office 提供的所有其他宏安全设置的补充。
此示例将从其所在的当前文档中提取代码 - 它本身是一个 VBA 宏(并将显示其自身以及任何其他代码)。 还有一个 Application.vbe.VBProjects 集合来访问其他文档。 虽然我从未这样做过,但我假设外部应用程序也可以使用此 VBProjects 集合打开文件。 安全性对于这些东西来说很有趣,所以它可能很棘手。
我还想知道现在的 docm 文件格式是什么 - 像 docx 这样的 XML? 这是一个更好的方法吗?
You'll have to add a reference to Microsoft Visual Basic for Applications Extensibility 5.3 (or whatever version you have). I have the VBA SDK and such on my box - so this may not be exactly what office ships with.
Also you have to enable access to the VBA Object Model specifically - see the "Trust Center" in Word options. This is in addition to all the other Macro security settings Office provides.
This example will extract code from the current document it lives in - it itself is a VBA macro (and will display itself and any other code as well). There is also a Application.vbe.VBProjects collection to access other documents. While I've never done it, I assume an external application could get to open files using this VBProjects collection as well. Security is funny with this stuff so it may be tricky.
I also wonder what the docm file format is now - XML like the docx? Would that be a better approach?