How can I start a specific excel version in com automation?

后端 未结 6 668
闹比i
闹比i 2021-01-07 07:19

I use Excel via COM automation (in c#), but it seems that the problem has no control over what version of excel is started on the box - we use both Excel 9 and Excel 11 and

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-07 07:44

    To get a specific Excel version you can start the binary executable directly using

    System.Diagnostics.Process process = System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office\Office11\Excel.exe");
    int processId = process.Id;
    

    This will give you the id of the Excel process. You can now connect to this Excel instance and can get hold of the object model by calling AccessibleObjectFromWindow.

    This method requires you to use P/Invoke and you also have to get a handle to the Excel window first. Since this is a little more work I will simply reference the following question where everything is described in detail:

    How to use use late binding to get excel instance?

提交回复
热议问题