以编程方式从 Word 2007 文档中提取宏 (VBA) 代码 [英] Programmatically extract macro (VBA) code from Word 2007 docs

查看:59
本文介绍了以编程方式从 Word 2007 文档中提取宏 (VBA) 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 API 从 Word 2007docm"文档中提取所有 VBA 代码?

Is it possible to extract all of the VBA code from a Word 2007 "docm" document using the API?

我已经找到了如何在运行时插入 VBA 代码,以及如何删除所有 VBA 代码,但没有将实际代码提取到我可以存储的流或字符串中(并在将来插入到其他文档中).

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.

编辑:感谢大家,Aardvark 的回答正是我想要的为了.我已将他的代码转换为 C#,并且能够使用 Visual Studio 2008 从类库中调用它.

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	est.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;
}

推荐答案

您必须添加对 Microsoft Visual Basic for Applications Extensibility 5.3(或您拥有的任何版本)的引用.我的盒子里有 VBA SDK 等 - 所以这可能不是 office 随附的.

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.

此外,您还必须专门启用对 VBA 对象模型的访问 - 请参阅 Word 选项中的信任中心".这是 Office 提供的所有其他宏安全设置的补充.

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.

此示例将从它所在的当前文档中提取代码 - 它本身是一个 VBA 宏(并且还会显示自身和任何其他代码).还有一个 Application.vbe.VBProjects 集合可以访问其他文档.虽然我从未这样做过,但我认为外部应用程序也可以使用此 VBProjects 集合打开文件.这些东西的安全性很有趣,所以它可能很棘手.

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.

我也想知道现在的 docm 文件格式是什么 - XML 就像 docx?那会是更好的方法吗?

I also wonder what the docm file format is now - XML like the docx? Would that be a better approach?

Sub GetCode()

    Dim prj As VBProject
    Dim comp As VBComponent
    Dim code As CodeModule
    Dim composedFile As String
    Dim i As Integer

    Set prj = ThisDocument.VBProject
        For Each comp In prj.VBComponents
            Set code = comp.CodeModule

            composedFile = comp.Name & vbNewLine

            For i = 1 To code.CountOfLines
                composedFile = composedFile & code.Lines(i, 1) & vbNewLine
            Next

            MsgBox composedFile
        Next

End Sub

这篇关于以编程方式从 Word 2007 文档中提取宏 (VBA) 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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