How to embed VBA code in Excel.xlsm without using Interop?

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

Random puzzle pieces found in my initial fact search: -I see that the 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 interaction and template with or without rows and columns

It would be nice to just have a VBA script file in a C # project that C # code extracts and pastes into an Excel document without VBA interaction. Any quick / quick / easy / easy way to do this or should I play with the free / commercial resources associated with the above?

+5
source share
2 answers

Using the OpenXML SDK 2.0 :

  • Create your macro and save it in .xlsm format, say snorehorse.xlsm.
  • Open snorehorse.xlsm in the Productivity Toolkit OpenXML and make the reflection code in the root of the tree.
  • Find the macro binary code. It is in string format and looks like random characters.
  • IDE OpenXML SDK excel, .
  • , №3, .
  • vba .
  • vba.
  • , .

:

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

DLL OpenXML SDK , SDK.

, , XML, OpenXML SDK, . - , .

, VBA script Excel, , . .

.

+6

EPPlus, VBA . . :

VBA Excel Excel.Interop

+1

All Articles