获取应用程序 ID
Get application ID
您知道如何获取应用程序 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);
}
您可以使用 Microsoft 的 OLE/COM Object Viewer.
找到此信息
You can use the OLE/COM Object Viewer to view a control's interfaces. Tell me more...
例如以 Word 为例,首先展开 All Objects 节点:
...然后向下滚动到 Microsoft Word 应用程序。与版本无关的 ProgID 将显示在右侧。在您的应用程序中使用它。
What if I will need Excel?
和以前一样,滚动直到找到 Microsoft Excel Application.
这里可以看到是Excel.Application。如果您安装了多个版本并希望使用特定版本,请使用 ProgID 字段中显示的 ProgId。
注意:如果像我一样遇到多个条目,请选择显示 ProgID 或 VersionIndependentProgID 的条目。
您知道如何获取应用程序 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);
}
您可以使用 Microsoft 的 OLE/COM Object Viewer.
找到此信息You can use the OLE/COM Object Viewer to view a control's interfaces. Tell me more...
例如以 Word 为例,首先展开 All Objects 节点:
...然后向下滚动到 Microsoft Word 应用程序。与版本无关的 ProgID 将显示在右侧。在您的应用程序中使用它。
What if I will need Excel?
和以前一样,滚动直到找到 Microsoft Excel Application.
这里可以看到是Excel.Application。如果您安装了多个版本并希望使用特定版本,请使用 ProgID 字段中显示的 ProgId。
注意:如果像我一样遇到多个条目,请选择显示 ProgID 或 VersionIndependentProgID 的条目。