using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Office = Microsoft.Office.Core;
using
Excel = Microsoft.Office.Interop.Excel;
using
VBIDE = Microsoft.Vbe.Interop;
namespace
Excel2008021000
{
class Program
{
static void Main(string[] args)
{
Excel.
Application oExcel = null; //Excel Application
Excel.
Workbook oBook = null; // Excel Workbook
Excel.
Sheets oSheetsColl = null; // Excel Worksheets collection
Excel.
Worksheet oSheet = null; // Excel Worksheet
Excel.
Range oRange = null; // Cell or Range in worksheet
VBIDE.
VBComponent oModule = null; // VBA Module
Office.
CommandBar oCommandBar = null;
Office.
CommandBarButton oCommandBarButton = null;
String sCode;
Object oMissing = System.Reflection.Missing.Value;
oExcel =
new Excel.Application(); // Create an instance of Excel.
oExcel.Visible =
true; // Make Excel visible to the user.
oExcel.UserControl =
true; // Set the UserControl property so Excel won't shut down.
System.Globalization.
CultureInfo ci = new System.Globalization.CultureInfo("en-US");
oBook = oExcel.Workbooks.Add(oMissing);
// Add a workbook.
oSheetsColl = oExcel.Worksheets;
// Get worksheets collection
oSheet = (Excel.
Worksheet)oSheetsColl.get_Item("Sheet1"); // Get Worksheet "Sheet1"
oRange = (Excel.
Range)oSheet.get_Range("E5", "E5"); // Get Range (or cell) "E5"
oRange.Value2 =
"Hi There!"; // Do something to oRange
// Create a new VBA code module.
// MUST GET Excel PERMISSION: see more at http://support.microsoft.com/kb/282830/
oModule = oBook.VBProject.VBComponents.Add(VBIDE.
vbext_ComponentType.vbext_ct_StdModule);
sCode =
"Sub VBAMacro()\r\n" +
" MsgBox \"VBA Macro called\"\r\n" +
"End Sub";
oModule.CodeModule.AddFromString(sCode);
// Add the VBA macro to the new code module.
// Run VBA macro "VBAMacro"
oBook.Application.Run(
"VBAMacro", oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
//wb.SaveAs(FileName, Excel.XlFileFormat.xlWorkbookNormal,
// null, null, false, false, Excel.XlSaveAsAccessMode.xlShared,
// false, false, null, null, null);
try
{
// Create a new toolbar and show it to the user.
oCommandBar = (Office.
CommandBar)oExcel.CommandBars.Add("VBAMacroCommandBar", oMissing, oMissing, true);
oCommandBar.Visible =
true;
// Create a new button on the toolbar.
oCommandBarButton = (Office.
CommandBarButton)oCommandBar.Controls.Add(Office.MsoControlType.msoControlButton, oMissing, oMissing, oMissing, oMissing);
oCommandBarButton.OnAction =
"VBAMacro"; // Assign a macro to the button.
oCommandBarButton.Caption =
"Call VBAMacro"; // Set the caption of the button.
oCommandBarButton.FaceId = 2151;
// Set the icon on the button to a picture.
}
catch (Exception eCBError) { }
// Release the variables.
oCommandBarButton =
null;
oCommandBar =
null;
oModule =
null;
oBook.Close(
false,oMissing,oMissing);
oBook =
null;
oExcel.Quit();
oExcel =
null;
// Collect garbage.
GC.Collect();
}
}
}
Something strange...
I deleted the references to Office 12 and added those to Office 11.
Then I saw that
1.- The code runs okay on a machine with Office 2003 (I can even get the new floating CommandBar)
2.- The code also runs on a machine with Office 2007 (without creating or showing the new Command Bar)
Doesn't it strange that references to Office 11 run on both systems and references to Office 12 run only under Office 2007?