Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8247399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:50:14+00:00 2026-06-07T22:50:14+00:00

I can successfully inject a piece of VBA code into a generated excel workbook,

  • 0

I can successfully inject a piece of VBA code into a generated excel workbook, but what I am trying to do is use the Workbook_Open() event so the VBA code executes when the file opens. I am adding the sub to the “ThisWorkbook” object in my xlsm template file. I then use the openxml productivity tool to reflect the code and get the encoded VBA data.

When the file is generated and I view the VBA, I see “ThisWorkbook” and “ThisWorkbook1” objects. My VBA is in “ThisWorkbook” object but the code never executes on open. If I move my VBA code to “ThisWorkbook1” and re-open the file, it works fine. Why is an extra “ThisWorkbook” created? Is it not possible to inject an excel spreadsheet with a Workbook_Open() sub? Here is a snippet of the C# code I am using:

private string partData = "...";  //base 64 encoded data from reflection code
//open workbook, myWorkbook
VbaProjectPart newPart = myWorkbook.WorkbookPart.AddNewPart<VbaProjectPart>("rId1");
System.IO.Stream data = GetBinaryDataStream(partData);
newPart.FeedData(data);
data.Close();
//save and close workbook

Anyone have ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:50:15+00:00Added an answer on June 7, 2026 at 10:50 pm

    Based on my research there isn’t a way to insert the project part data in a format that you can manipulate in C#. In the OpenXML format, the VBA project is still stored in a binary format. However, copying the VbaProjectPart from one Excel document into another should work. As a result, you’d have to determine what you wanted the project part to say in advance.

    If you are OK with this, then you can add the following code to a template Excel file in the ‘ThisWorkbook’ Microsoft Excel Object, along with the appropriate Macro code:

    Private Sub Workbook_Open()
        Run "Module1.SomeMacroName()"
    End Sub
    

    To copy the VbaProjectPart object from one file to the other, you would use code like this:

    public static void InsertVbaPart()
    {
        using(SpreadsheetDocument ssDoc = SpreadsheetDocument.Open("file1.xlsm", false))
        {
            WorkbookPart wbPart = ssDoc.WorkbookPart;
            MemoryStream ms;
            CopyStream(ssDoc.WorkbookPart.VbaProjectPart.GetStream(), ms);
    
            using(SpreadsheetDocument ssDoc2 = SpreadsheetDocument.Open("file2.xlsm", true))
            {
                Stream stream = ssDoc2.WorkbookPart.VbaProjectPart.GetStream();
                ms.WriteTo(stream);
            }
        }
    }
    
    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[short.MaxValue + 1];
        while (true)
        {
            int read = input.Read(buffer, 0, buffer.Length);
            if (read <= 0)
                return;
            output.Write(buffer, 0, read);
        }
    }
    

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can successfully create QR Code PNG images with ZXing but there is no
I have a Spring Project with Wicket. I can successfully inject Services in Wicket
In my project I can successfully test database code. I'm using Spring, Hibernate, HSQLDB,
I have a page in which i can successfully use the jquery focus function
Ok I can successfully connect to my Access database but I am still learning
I have a BroadcastReceiver that can successfully catch a broadcast, but if I try
In my Silverlight RIA app I can successfully inject an ExceptionHelper class via constructor
I can successfully compiled and run the Hello World code. Now I want to
I can successfully download files from server using the macro below, but sometimes the
I can successfully send emails by using CakeEmail class of CakePHP. But how can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.