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

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

问题描述

是否有可能从Word 2007中提取所有的VBA codeDOCM使用API​​的文档?

我已经找到如何将VBA code在运行时,如何删除所有的VBA code,但拉不实际的code OUT到流或字符串,我可以存储(并插入成在将来其他文件)。

任何提示或资源将接入点preciated。

修改:感谢大家,土豚的回答是正是我一直在寻找。我已经将他的code到C#,并能够使用Visual Studio 2008的一个类库调用它。

 使用的Microsoft.Office.Interop.Word;
使用Microsoft.Vbe.Interop;

...

公开名单<字符串> GetMacrosFromDoc()
{
    文档DOC = GetWordDoc(@C:\ TEMP \ test.docm);

    名单<字符串>宏=新的名单,其中,串>();

    VBProject PRJ;
    codeModule code;
    串composedFile;

    PRJ = doc.VBProject;
    的foreach(的VBComponent补偿在prj.VBComponents)
    {
        code =补偿codeModule。

        //把code模块的名称上方
        composedFile = comp.Name + Environment.NewLine;

        //循环遍历(1-索引)线
        的for(int i = 0; I< code.CountOfLines;我++)
        {
            composedFile + = code.get_Lines(我+ 1,1)+ Environment.NewLine;
        }

        //添加宏到列表
        macros.Add(composedFile);
    }

    CloseDoc(DOC);

    返回宏;
}
 

解决方案

您必须添加一个引用到Microsoft Visual Basic应用程序扩展5.3(或任何版本,你有)。我有VBA SDK和这样的对我的盒子 - 所以这可能不是什么办公室附带了

此外,您必须启用对VBA对象模型专门 - 看到在Word选项信任中心。这是除了所有其他宏安全设置办公室提供。

这个例子将提取code从它生活在当前文档 - 这本身就是一个VBA宏(并显示自己和其他任何code以及)。还有一个Application.vbe.VBProjects集合访问的其他文件。虽然我从来没有做过它,我假定外部应用程序可以得到打开使用该VBProjects收藏及文件。安全是有趣的这个东西,所以它可能会非常棘手。

我也想知道是什么DOCM文件格式是现在 - XML喜欢的docx?那会是一个更好的办法?

 子获取code()

    昏暗的PRJ作为VBProject
    昏暗的补偿作为的VBComponent
    昏暗code以codeModule
    昏暗composedFile作为字符串
    昏暗我作为整数

    设置PRJ = ThisDocument.VBProject
        对于每一个补偿在prj.VBComponents
            设置code =补偿。codeModule

            composedFile = comp.Name和放大器; vbNewLine

            对于i = 1到code.CountOfLines
                composedFile = composedFile和放大器; code.Lines(1,1)及vbNewLine
            下一个

            MSGBOX composedFile
        下一个

结束小组
 

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

解决方案

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?

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

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

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