How to programmably add a macro to an Excel workbook using VSTO c# code


  • Hi everybody,
    I have a question concerning how to programmably add a macro to an Excel workbook using VSTO c# code.
    Basically what I want to do is: I have a VSTO add-in and based on some condiction it will create a new Excel workbook, and I want the current VSTO add-in to also programmably add a macro to this newly created workbook so that even  other people who don't have the VSTO installed on their PC can still use the functionality that this macro can provide.
    Actually the origin of this question is: my VSTO add-in will extract some information from an Excel workbook(lets say "Original workbook") and save this data in a newly created Excel workbook(lets say "New workbook"). Those New workbooks may be examined by some other people. I want to facilitate the examine process by letting the user double the cells in the New workbook and the focus will automatically go to a particular row in the original Workbook so that they can reference the detailed information in the original workbook. This can be easily done by writing a VSTO application-level add-in, but it requires that the examiners have the VSTO installed on the PC. This should also be easy to do using VBA code or a macro, but I need to include the VBA code along with those New workbook document. So to achieve both, I'm thinking that in my current VSTO add-in I programmably add a macro or inserting VBA code into the newly created workbook. But I don't know how to implement it in C#  or whether it is feasible to do this.

    I searched on the web for some time and haven't got the results yet. So I greatly appreciate anybody who can help me with this. Many thanks in advance!
    Tuesday, March 17, 2009 12:19 AM


All replies