HOWTO:在 Visual C# .NET 中使用自動化創建 Excel 宏

HOWTO:在 Visual C# .NET 中使用自動化創建 Excel 宏

<script type=text/javascript>function loadTOCNode(){}</script>
文章編號 : 303872
最後修改 : 2004年2月13日
修訂 : 5.0
本文的發佈號曾爲 CHS303872
<script type=text/javascript> var sectionFilter = "type != 'notice' && type != 'securedata' && type != 'querywords'"; var tocArrow = "/library/images/support/kbgraphics/public/en-us/downarrow.gif"; var depthLimit = 10; var depth3Limit = 10; var depth4Limit = 5; var depth5Limit = 3; var tocEntryMinimum = 1; </script> <script src="/common/script/gsfx/kbtoc.js??4" type=text/javascript></script>

概要

<script type=text/javascript>loadTOCNode(1, 'summary');</script>
本文逐步介紹如何在 Microsoft Visual C# .NET 中使 Microsoft Excel 自動運行以創建包含新宏(該宏與 CommandBar 按鈕關聯)的工作簿。

更多信息

<script type=text/javascript>loadTOCNode(1, 'moreinformation');</script>

創建 Visual C# .NET 應用程序示例的步驟

<script type=text/javascript>loadTOCNode(2, 'moreinformation');</script>
1. 啓動 Microsoft Visual Studio .NET。
2. 文件菜單上,單擊新建,然後單擊項目。從 Visual C# 項目類型中選擇 Windows 應用程序。默認情況下會創建 Form1。
3. 添加對 Microsoft Excel 對象庫Microsoft Visual Basic for Applications 擴展庫的引用。爲此,請按照下列步驟操作:
a. 項目菜單上,單擊添加引用
b. COM 選項卡上,找到 Microsoft Excel 對象庫,然後單擊選擇。然後找到 Microsoft Visual Basic for Applications 擴展庫並單擊選擇

注意:Microsoft Office 2003 包含主 Interop 程序集 (PIA)。Microsoft Office XP 不包含 PIA,但您可以下載 PIA。 有關 Office XP PIA 的其他信息,請單擊下面的文章編號,以查看 Microsoft 知識庫中相應的文章:
328912 (http://support.microsoft.com/kb/328912/) INFO: Microsoft Office XP PIA 可供下載
c. 添加引用對話框中單擊確定以接受您的選擇。
4. 視圖菜單上,選擇工具箱以顯示工具箱,然後向 Form1 添加一個按鈕。
5. 雙擊 Button1。代碼窗口打開並顯示 Button1Click 事件。將以下代碼行添加到代碼窗口頂部的 using 語句中:
using Office = Microsoft.Office.Core;
using VBIDE = Microsoft.Vbe.Interop;
using Excel = Microsoft.Office.Interop.Excel;
					
6. 在代碼窗口中,將以下代碼
private void button1_Click(object sender, System.EventArgs e)
{
}
					
替換爲:
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("VBAMacroCommandBar",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 = "VBAMacro";
		// Set the caption of the button.
		oCommandBarButton.Caption = "Call VBAMacro";
		// Set the icon on the button to a picture.
		oCommandBarButton.FaceId = 2151;
	} 
	catch(Exception eCBError) {
		MessageBox.Show("VBAMacroCommandBar 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();
}
					
7. 按 F5 鍵生成並運行該程序。
8. 單擊 Button1 啓動 Microsoft Excel,插入 Visual Basic for Applications (VBA) 代碼,然後添加一個新的 CommandBar。單擊 CommandBar 上的按鈕以運行 VBA 宏。

Additional Notes for Office XP

<script type=text/javascript>loadTOCNode(2, 'moreinformation');</script> Microsoft Office 2003 和 Microsoft Office XP 應用程序具有一個安全選項,以允許對 VBA 對象模型進行編程訪問。如果此設置爲(默認設置),可能會在運行該代碼示例時出現一個錯誤。 有關此設置和如何糾正該錯誤的其他信息,請單擊下面的文章編號,以查看 Microsoft 知識庫中相應的文章:
282830 (http://support.microsoft.com/kb/282830/) PRB:Programmatic Access to Office XP VBA Project Is Denied

參考

<script type=text/javascript>loadTOCNode(1, 'references');</script>
有關其他信息,請單擊下面的文章編號,以查看 Microsoft 知識庫中相應的文章:
303871 (http://support.microsoft.com/kb/303871/) Create an Excel Macro Programmatically from Visual Basic .NET
194611 (http://support.microsoft.com/kb/194611/) Create and Call an Excel Macro Programmatically from VB

這篇文章中的信息適用於:
Microsoft Visual C# .NET 2003 標準版
Microsoft Visual C# .NET 2002 標準版
Microsoft Office Excel 2003
Microsoft Excel 2002 標準版
關鍵字: 
kbhowto kbpia KB303872
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章