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

发布于 2024-12-29 03:45:17 字数 623 浏览 2 评论 0原文

我使用 VSTO 填充 Excel 工作表,如下所示:

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

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

上周,当我第一次创建代码时,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 中的某种首选项,还是我在创建应用程序实例时必须指定的内容?

I am population an Excel sheet using VSTO as follow:

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

The sheet is created and everything is fine, except that I have two versions of Excel installed in the envrionment I work in (Excel 2003 and Excel 2010).

Last week, when I first created the code, Excel 2010 was showing up. However, this week, it's Excel 2003 that opens up.

My project references Microsoft.Office.Interop.Excel with the latest version (C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll, Version 14).

Is this somehow a preference in Windows, or is it something I have to specify when I create the instance of the application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往昔成烟 2025-01-05 03:45:17

也许启动 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; 

}

maybe start excel 2010 and get handle to it, below code may help

 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; 

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文