For Microsoft Visual Basic .NET versions of this article, see
303871.
This task content
Summary
Steps to create a Visual C # .NET application example: Additional instructions for Office XP
SUMMARY This article is gradually introducing how to automatically run Microsoft Excel in Microsoft Visual C # .NET to create a new macro (this macro
The workbook associated with the CommandBar button.
Back to top
More information
Steps to create a Visual C # .NET application example
Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Select a Windows application from the Visual C # project type. FORM1 is created by default. Add a reference to the Microsoft Excel object library and the Microsoft Visual Basic for Applications extension library. To do this, follow these steps:
On the project menu, click Add Reference. On the COM tab, find the Microsoft Excel object library, then click Select. Then find the Microsoft Visual Basic for Applications extension library and click Select. Note: Microsoft Office 2003 contains the main intero set (PIA). Microsoft Office XP does not contain PIA, but you can download PIA. For additional information about Office XP PIA, click the article number below to view the article in the Microsoft Knowledge Base: 328912 Info: Microsoft Office XP PIA is available for download in the Add Reference dialog box Click OK to accept your choice . On the View menu, select the Toolbox to display the toolbox, then add a button to Form1. Double click on Button1. The code window opens and displays the Click event of Button1. Add the following code line to the USING statement at the top of the code window: use office = microsoft.Office.core;
Using vbide = microsoft.vbe.interop;
Using Excel = Microsoft.Office.Interop.Excel;
In the code window, put the following code private void button1_click (Object Sender, System.EventArgs E)
{
}
Replace 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 =
"SUB VBAMACRO () / R / N"
"msgbox /" VBA Macro Called / "R / N"
"End Sub";
// 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 ("Vbamacrocman), OMISSING, OMISSING, /);
Ocommandbar.visible = true;
// Create a New Button on The Toolbar.
Ocommandbarbutton = (office.commandbarbutton) Ocommandbar.controls.add (
Office.msocontrolType.msocontrolbutton,
OMISSING, OMISSING, OMISING, OMISING
// Assign a macro to the button.
OcommandbarButton.onAction = "vbamacro";
// set the capen of the button.
Ocommandbarbutton.caption = "Call vbamacro";
// set the icon on the button to a picture.
Ocommandbarbutton.faceId = 2151;
}
Catch (Exception EcBerror) {
MessageBox.show ("VBamacrocmanDBar Already Exists", "Error");
}
// 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 ();
}
Press F5 to build and run the program. Click Button1 to launch Microsoft Excel, insert the Visual Basic for Applications (VBA) code, then add a new CommandBar. Click the button on Commandbar to run the VBA macro.
Back to top
The Additional Notes for Office XPMICROFT Office 2003 and Microsoft Office XP applications have a security option to allow program access to the VBA object model. If this setting is
Off (default setting), an error may occur when running the code sample. For additional information about this setting and how to correct the error, click the article number below to see the article in the Microsoft Knowledge Base: 282830 PRB: Programmatic Access TO Office XP VBA Project Is Denied
Back to top
Refer to additional information, click the article number below to see the article in the Microsoft Knowledge Base:
303871 Create An Excel Macro Programmatically from visual Basic .NET
194611 Create and Call An Excel Macro Programmatically from VB
Back to top
The information in this article applies to:
Microsoft Visual C # .NET (2003) Microsoft Visual C # .NET (2002) Microsoft Office Excel 2003 Microsoft Excel 2002