Microsoft KB Archive/303872

= How To Create an Excel Macro by Using Automation from Visual C# .NET =

Article ID: 303872

Article Last Modified on 1/30/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition

-



This article was previously published under Q303872



For a Microsoft Visual Basic .NET version of this article, see 303871.

IN THIS TASK

 * SUMMARY
 * Steps to Create the Sample Visual C# .NET Application
 * Additional Notes for Office XP
 * REFERENCES



SUMMARY
This step-by-step article describes how to automate Microsoft Excel by using Microsoft Visual C# .NET to create a workbook that contains a new macro that is associated with a CommandBar button.

back to the top



Steps to Create the Sample Visual C# .NET Application
 Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Select Windows Application from the Visual C# Project types. Form1 is created by default. Add a reference to Microsoft Excel Object Library and Microsoft Visual Basic for Applications Extensibility Library. To do this, follow these steps:  On the Project menu, click Add Reference. On the COM tab, locate the Microsoft Excel Object Library, and then click Select. Then locate the Microsoft Visual Basic for Applications Extensibility Library, and then click Select.

Note Microsoft Office 2003 includes Primary Interop Assemblies (PIAs). Microsoft Office XP does not include PIAs, but they can be downloaded. For additional information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:

328912 INFO: Microsoft Office XP PIAs Are Available for Download

 Click OK in the Add References dialog box to accept your selections.</li></ol> </li> On the View menu, select Toolbox to display the Toolbox, and add a button to Form1.</li>  Double-click Button1. The code window opens at the Click event for Button1. Add the following lines of code to the using statements at the top of the code window: using Office = Microsoft.Office.Core; using VBIDE = Microsoft.Vbe.Interop; using Excel = Microsoft.Office.Interop.Excel; </li>  In the code window, replace the following code private void button1_Click(object sender, System.EventArgs e) { } with: private void button1_Click(object sender, System.EventArgs e) { Excel.Application oExcel; Excel.Workbook oBook; VBIDE.VBComponent oModule; Office.CommandBar oCommandBar; Office.CommandBarButton oCommandBarButton; String sCode; Object oMissing = System.Reflection.Missing.Value;

// Create an instance of Excel. oExcel = new Excel.Application;

// Add a workbook. oBook = oExcel.Workbooks.Add(oMissing);

// Create a new VBA code module. oModule = oBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule); sCode = &quot;sub VBAMacro\r\n&quot; + &quot;  msgbox \&quot;VBA Macro called\&quot;\r\n&quot; + &quot;end sub&quot;; // Add the VBA macro to the new code module. oModule.CodeModule.AddFromString(sCode);

try {       // Create a new toolbar and show it to the user. oCommandBar = oExcel.CommandBars.Add(&quot;VBAMacroCommandBar&quot;,oMissing, oMissing,\); oCommandBar.Visible = true; // Create a new button on the toolbar. oCommandBarButton = (Office.CommandBarButton) oCommandBar.Controls.Add(           Office.MsoControlType.msoControlButton,            oMissing, oMissing, oMissing, oMissing); // Assign a macro to the button. oCommandBarButton.OnAction = &quot;VBAMacro&quot;; // Set the caption of the button. oCommandBarButton.Caption = &quot;Call VBAMacro&quot;; // Set the icon on the button to a picture. oCommandBarButton.FaceId = 2151; }    catch(Exception eCBError) { MessageBox.Show(&quot;VBAMacroCommandBar already exists.&quot;,&quot;Error&quot;); }

// Make Excel visible to the user. oExcel.Visible = true; // Set the UserControl property so Excel won't shut down. oExcel.UserControl = true;

// Release the variables. oCommandBarButton = null; oCommandBar = null; oModule = null; oBook = null; oExcel = null; // Collect garbage. GC.Collect; }                   </li> Press F5 to build and to run the program.</li> Click Button1 to start Microsoft Excel, insert Microsoft Visual Basic for Applications (VBA) code, and then add a new CommandBar. Click the button on the CommandBar to run the VBA macro.</li></ol>

back to the top

Additional Notes for Office XP
Microsoft Office 2003 and Microsoft Office XP applications have a security option to allow programmatic access to the VBA object model. If this setting is Off (the default), you may receive an error running the sample code. For additional information on this setting and how you can correct the error, click the following article number to view the article in the Microsoft Knowledge Base:

282830 PRB: Programmatic Access to Office XP VBA Project Is Denied

back to the top

<div class="references_section">