如何确保使用VSTO 4打开哪个版本的Excel?

我是一个使用VSTO的Excel工作表,如下所示:

Application app = new Application(); var wBook = app.Workbooks.Add(); var wSheet = (wBook.Worksheets[1] as Worksheet); /* Population algorithm */ app.Visible=true; 

工作表被创build,一切都很好,除了我在工作环境(Excel 2003和Excel 2010)中安装了两个版本的Excel。

上个星期,当我第一次创build代码时,Excel 2010显示出来了。 但是,本周,Excel 2003打开了。

我的项目引用Microsoft.Office.Interop.Excel与最新版本( C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll ,版本14)。

这在某种程度上是Windows中的首选项,还是我创build应用程序实例时必须指定的东西?

也许开始Excel 2010并得到处理,下面的代码可能会有所帮助

  public static void GetReferences(ref Microsoft.Office.Interop.Excel.Application _Application, ref Microsoft.Office.Interop.Excel.Workbook _Workbook) { EnumChildCallback cb; // start exe int hwnd = Process.Start("Excel 2010 path").MainWindowHandle; if (hwnd != 0) { int hwndChild = 0; cb = new EnumChildCallback(EnumChildProc); EnumChildWindows(hwnd, cb, ref hwndChild); if (hwndChild != 0) { const uint OBJID_NATIVEOM = 0xFFFFFFF0; Guid IID_IDispatch = new Guid( "{00020400-0000-0000-C000-000000000046}"); Microsoft.Office.Interop.Excel.Window ptr = null; int hr = AccessibleObjectFromWindow( hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref ptr); if (hr >= 0) { _Application = ptr.Application; _Workbook = _Application.ActiveWorkbook; } } } } [DllImport("Oleacc.dll")] public static extern int AccessibleObjectFromWindow( int hwnd, uint dwObjectID, byte[] riid, ref Microsoft.Office.Interop.Excel.Window ptr); public delegate bool EnumChildCallback(int hwnd, ref int lParam); [DllImport("User32.dll")] public static extern bool EnumChildWindows( int hWndParent, EnumChildCallback lpEnumFunc, ref int lParam); [DllImport("User32.dll")] public static extern int GetClassName( int hWnd, StringBuilder lpClassName, int nMaxCount); public static bool EnumChildProc(int hwndChild, ref int lParam) { StringBuilder buf = new StringBuilder(128); GetClassName(hwndChild, buf, 128); if (buf.ToString() == "EXCEL7") { lParam = hwndChild; return false; } return true; 

}