Visual C# .NET 使用自动化运行 Office 宏
HOW TO: Run Office Macros by Using Automation from Visual C# .NET
| Article ID | : | 306683 |
| Last Review | : | May 5, 2006 |
| Revision | : | 6.0 |
This article was previously published under Q306683
On This Page
SUMMARY
This step-by-step article describes how to call Office macros from a Visual C# .NET Automation client.
You can use Microsoft Office Automation to open a document or create a new document that contains a Visual Basic for Applications (VBA) macro and execute the macro at run time.
The following sample Automation client manipulates an Office Automation server (Access, Excel, PowerPoint, or Word) based on your selection on a form. After the client starts the Automation server, it opens a document, and then calls two macros. The first macro, DoKbTest, has no parameters and the second macro, DoKbTestWithParameter, takes a single parameter of type String.
For additional information about how to resolve this error so that you can successfully reference the Access 10.0 object library, click the article number below to view the article in the Microsoft Knowledge Base:
You can use Microsoft Office Automation to open a document or create a new document that contains a Visual Basic for Applications (VBA) macro and execute the macro at run time.
The following sample Automation client manipulates an Office Automation server (Access, Excel, PowerPoint, or Word) based on your selection on a form. After the client starts the Automation server, it opens a document, and then calls two macros. The first macro, DoKbTest, has no parameters and the second macro, DoKbTestWithParameter, takes a single parameter of type String.
Create Office Documents That Contain Macros
| 1. | Create a Word document named C:\Doc1.doc. To do this, follow these steps:
|
||||||||||
| 2. | Create an Excel workbook named C:\Book1.xls by using steps similar to those that you used to create the Word document. | ||||||||||
| 3. | Create a PowerPoint presentation named C:\Pres1.ppt by using steps similar to those that you used to create the Word document. | ||||||||||
| 4. | Create a new Access database named C:\Db1.mdb. To do this, follow these steps:
|
Create the Visual C# .NET Automation Client
| 1. | Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual C# Projects, then click Windows Application under Templates. Form1 is created by default. | ||||||||
| 2. | Add a reference to the Access, Excel, PowerPoint, and Word object libraries. To do this, follow these steps:
|
||||||||
| 3. | On the View menu, click ToolBox. Add a Combo Box control and a Button control to Form1. | ||||||||
| 4. | Double-click Button1 to generate a definition for the Click event handler of the button. | ||||||||
| 5. | Paste the following code in the button1_Click handler:
|
||||||||
| 6. | Add the following function after the button1_Click handler:
|
||||||||
| 7. | On the View menu, click Designer and then double-click Form1 to generate a definition for the Load event of the form. | ||||||||
| 8. | Paste the following code in the Form1_Load handler:
|
||||||||
| 9. | Scroll to the top of the code window, and then add the following lines of code to the end of the list of using directives:
|
Run and Test the Automation Client
| 1. | Press F5 to run the application. |
| 2. | Select an Office application from ComboBox1, and then click Button1. The Office application that you selected is started and the DoKBTest and DoKBTestWithParameter macros are run. |
Troubleshooting
When you reference the Access 10.0 object library in a Visual C# .NET project, you may receive an error message that states that conversion of the library to a .NET assembly failed.For additional information about how to resolve this error so that you can successfully reference the Access 10.0 object library, click the article number below to view the article in the Microsoft Knowledge Base:
317157 (http://support.microsoft.com/kb/317157/EN-US/) PRB: Errors When Referencing the Microsoft Access 10.0 Type Library
REFERENCES
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
303872 (http://support.microsoft.com/kb/303872/EN-US/) HOWTO: Create an Excel Macro by Using Automation from Visual C# .NET
177760 (http://support.microsoft.com/kb/177760/EN-US/) VBA: How to Run Macros in Other Office Programs
For more information and resources about Office Automation, visit the following Microsoft Web sites:
Microsoft Office Development with Visual Studio
http://msdn.microsoft.com/library/en-us/odc_2003_ta/html/odc_ancVSTO.asp (http://msdn.microsoft.com/library/en-us/odc_2003_ta/html/odc_ancVSTO.asp)
FAQs and Highlights for Office Development
http://support.microsoft.com/ofd (http://support.microsoft.com/ofd)
http://msdn.microsoft.com/library/en-us/odc_2003_ta/html/odc_ancVSTO.asp (http://msdn.microsoft.com/library/en-us/odc_2003_ta/html/odc_ancVSTO.asp)
FAQs and Highlights for Office Development
http://support.microsoft.com/ofd (http://support.microsoft.com/ofd)

Back to the top
浙公网安备 33010602011771号