查找 Office 应用程序的现有实例

Find existing instance of Office Application

有什么方法可以将现有的 MS Publisher 实例获取为 Microsoft.Office.Interop.Publisher.Application

我找到了这个:

System.Diagnostics.Process.GetProcessesByName("Microsoft Publisher")

所以我可以检查这是否已经是 运行,但如何将其转换为 MS Publisher 应用程序?所以我可以调用 Microsoft.Office.Interop.Publisher.Application.Open 例如?

你可以试试这个 Microsoft getActiveObject。这是一个例子。

    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);
    }

希望我有所帮助!