我将如何注入VBA代码到Excel .XLSM不使用VBA项目对象模块互操作? [英] How would I inject VBA code into Excel .xlsm without using VBA Project Object Module Interop?

查看:429
本文介绍了我将如何注入VBA代码到Excel .XLSM不使用VBA项目对象模块互操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是宏添加到Excel工作簿,而无需启用Excel的信任中心的VBA项目对象模块信任访问。 (启用访问似乎是一个安全隐患)



发现我其实最初的拼图碎片随机搜索发现:
-I看到VBA脚本存储在压缩.XLSM文件vbaProject.bin。
- 有几个自由/商业资源,可以使用Excel文件的工作:
没有互操作和模板创建EXCEL带或不带行和columnspan



这将是很好只需在该C#代码拉和注入到没有VBA互操作的Excel文档的C#项目中的VBA脚本文件。任何快速/快速/简单/直接的方式做到这一点,或者我应该带有链接上面?


解决方案
<自由/商业资源玩耍p>想出了一个办法使用的OpenXML SDK 2.0



一般步骤:




  1. 创建宏代码并将其保存在.XLSM格式,说snorehorse.xlsm。

  2. 在OpenXML的生产力工具包打开snorehorse.xlsm并做树根一个反映代码。

  3. 查找宏的二进制代码。它在一个字符串格式,看起来就像是随机的字符。

  4. 在IDE中,添加到OpenXML的SDK的引用,并以编程方式创建或打开Excel文件要注入的宏代码。

  5. 在步骤3中找到的宏字符串复制到你的代码。

  6. 一个新的VBA部分添加到目的地。

  7. 字符串数据送入新的VBA部分。

  8. 保存和运行,并很高兴你绕过信托中心。



示例代码:

 使用DocumentFormat.OpenXml; 
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Spreadsheet;

私人字符串partData =...;

公共无效vbaInjector {
[代码中使用的OpenXML创建/打开电子表格省略]
VbaProjectPart vbaProjectPart1 = snoreSpreadsheetDoc.WorkbookPart.AddNewPart< VbaProjectPart>(rId8);
的System.IO.Stream数据= GetBinaryDataStream(partData);
vbaProjectPart1.FeedData(数据);
data.Close();
[代码关闭电子表格和清理省略]
}


私人的System.IO.Stream GetBinaryDataStream(字符串base64String)
{
返回新System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
}



我选择的OpenXML的SDK DLL添加到项目的本地版本,以便最终用户不必安装SDK本身。



我觉得这可以在一个较低的水平来完成,使用XML工作,而无需使用的OpenXML SDK,但我没有试图去学习如何做到这一点。如果任何人都可以张贴代码,我接受过我这个问题的答案。



此外,如果一个人有一个编程的方式来VBA脚本,转换成一个嵌入的资源文件,成格式的Excel预期的二进制字符串,我们可以绕过不必每次你想改变宏代码的时间复制和二进制数据的一个新的字符串粘贴。这将是一个卓越的回答我的。



感谢。


My goal is to add macros to an excel workbook without having to enable "Trust Access to the VBA Project Object Module" in the Excel Trust Center. (Enabling access seems a security risk).

Found random pieces of puzzle on my initial fact finding search: -I see that VBA script is stored in the zipped .xlsm file as vbaProject.bin. -There are several free/commercial resources that can work with excel files: Create excel without interop and template with or without row and columnspan

It would be nice to simply have a file of VBA script in the C# project that the C# code pulls from and injects into the Excel document without VBA interop. Any quick/fast/simple/straightforward way to do this or should I play around with the free/commercial resources linked to above?

解决方案

Came up with a way to do it using OpenXML SDK 2.0.

General Steps:

  1. Create your macro code and save it in .xlsm format, say snorehorse.xlsm.
  2. Open snorehorse.xlsm in the OpenXML Productivity Toolkit and do a Reflect Code on the tree root.
  3. Find the macro's binary code. It's in a string format and looks like random characters.
  4. In your IDE, add a reference to OpenXML SDK, and programmatically create or open the excel file you want to inject the macro code into.
  5. Copy the macro string found in step #3 into your code.
  6. Add a new vba part to the destination.
  7. Feed the string data into the new vba part.
  8. Save and run and be pleased you bypassed the Trust Center.

Example code:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

private string partData = "...";

    public void vbaInjector{    
        [code to create / open spreadsheet using OpenXML omitted]
        VbaProjectPart vbaProjectPart1 = snoreSpreadsheetDoc.WorkbookPart.AddNewPart<VbaProjectPart>("rId8");
        System.IO.Stream data = GetBinaryDataStream(partData);
        vbaProjectPart1.FeedData(data);
        data.Close();
        [code to close spreadsheet and cleanup omitted]
    }


    private System.IO.Stream GetBinaryDataStream(string base64String)
    {
        return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
    }

I chose to add the OpenXML SDK dll into the project's local build so the end users won't have to install the SDK themselves.

I think this can be done on a lower level, working with the XML, without using the OpenXML SDK, but I haven't attempted to learn how to do this. If anyone can post the code, I'll accept that Answer over mine.

Also, if one had a programmatic way to convert VBA script, in an embedded resource file, into a binary string of the format excel expects, one could bypass having to copy and paste in a new string of binary data every time you wanted to change the macro code. That would be a superior answer to mine.

Thanks.

这篇关于我将如何注入VBA代码到Excel .XLSM不使用VBA项目对象模块互操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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