获取应用程序ID

时间:2015-10-12 07:33:54

标签: c# windows com system ole

您知道如何获取应用程序ID(" Word.Application"在给定示例中)?如果我需要Excel怎么办?还是InDesing?

object word;
try
{
    word = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
//If there is a running Word instance, it gets saved into the word variable
}
catch (COMException)
{
//If there is no running instance, it creates a new one
    Type type = Type.GetTypeFromProgID("Word.Application");
    word = System.Activator.CreateInstance(type);
}

Find existing instance of Office Application

1 个答案:

答案 0 :(得分:1)

您可以使用Microsoft的 OLE / COM对象查看器找到此信息。

  

您可以使用OLE / COM对象查看器查看控件的界面。 Tell me more...

e.g。通过Word示例,首先展开所有对象节点:

enter image description here

...然后向下滚动到 Microsoft Word应用程序。版本无关的ProgID将显示在右侧。在您的应用程序中使用它。

enter image description here

  

如果我需要Excel怎么办?

和以前一样,滚动直到找到 Microsoft Excel应用程序

enter image description here

在这里,我们可以看到它是 Excel.Application 。如果您安装了多个版本并希望使用特定版本,请使用ProgId,如 ProgID 字段中所示。

注意:如果您和我一样遇到多个条目,请选择显示 ProgID VersionIndependentProgID 的条目。

相关问题