Using OpenXML SDK 2.0:
- Create your macro code and save it in .xlsm format, say snorehorse.xlsm.
- Open snorehorse.xlsm in the OpenXML Productivity Toolkit and do a Reflect Code on the tree root.
- Find the macro's binary code. It's in a string format and looks like random characters.
- 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.
- Copy the macro string found in step #3 into your code.
- Add a new vba part to the destination.
- Feed the string data into the new vba part.
- 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.