C# 如何在word应用程序中查找外接程序列表

C# 如何在word应用程序中查找外接程序列表,c#,vsto,add-in,C#,Vsto,Add In,我已经使用VisualStudioToolsforOffice(VSTO)创建了一个office:word加载项。 我已将外接程序的loadehavior修改为“0”,以停止其自动加载行为 我的要求是从c#应用程序启动word文档,并仅为此word实例启用外接程序 Using Word = Microsoft.Office.Interop.Word; { Word.Application wordApp; //Instantiate a word application word

我已经使用VisualStudioToolsforOffice(VSTO)创建了一个office:word加载项。 我已将外接程序的loadehavior修改为“0”,以停止其自动加载行为

我的要求是从c#应用程序启动word文档,并仅为此word实例启用外接程序

Using Word = Microsoft.Office.Interop.Word;

{
  Word.Application wordApp;

  //Instantiate a word application
  wordApp = new Word.Application();
  wordApp.visible = true;

  // Open a document
  wordApp.Documents.Open(ref wordFile, ref Missing.value, ..... etc );  

  foreach (Word.AddIns addins in wordApp.Application.AddIns)
       MessageBox.Show(addins.ToString());
}
for循环引发异常:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Word.AddIn'  
*如何获取/存储/迭代加载项/组件列表*


关于,

我终于找到了解决问题的方法:

// This will return all the word addins
Microsoft.Office.Core.COMAddIns comAddins = wordApp.COMAddIns;

// Iterate through all the addins 
for(Microsoft.Office.Core.COMAddIns addins in wordApp.COMAddIns)
     MessageBox.Show(addin.Description);

众所周知,应用程序级加载项适用于特定应用程序的所有实例。我成功地为Office 2007应用程序的特定实例(Word和Excel)启用了应用程序级加载项。例如,如果我从c#应用程序启动word实例,我的应用程序级加载项(自定义功能区功能)将仅应用于该实例,则手动启动的所有其他实例的行为都是正常的

每个应用程序级外接程序都在注册表中注册自身。因此,应用程序的每个实例都会尝试加载外接程序。因此,主要的工作是加载ribbon

在运行时,您必须决定是加载自定义功能区还是加载基本功能区

为此,
->在c#应用程序中创建一个特定于流程的环境变量,您可以在其中实例化office应用程序(word/excel)

->检查外接程序的ribbon类中的变量。 如果变量存在,则加载自定义功能区;如果变量不存在,则加载基本功能区

public string GetCustomUI(string ribbonID)
{
   if (System.Environment.GetEnvironmentVariable("MyVar", EnvironmentVariableTarget.Process) == "1")
   {
        return GetResourceText("ExcelAddIn.ExcelRibbon.xml");
   }
   else
   {
        return GetResourceText("ExcelAddIn.BasicRibbon.xml");
   }
}
你差不多完了!。但是windows不允许您一次维护两个word/excel实例(.ex)。因此,每个word/excel实例将从相同的.EXE打开,并且您的加载项将应用于所有实例。因此,将word/excel的每个实例(.exe)分开

有注册表黑客来实现这一点:
关键是,

HKEY_CLASSES_ROOT\Word.Document.12\shell\Open\Command
HKEY_CLASSES_ROOT\Word.Document.12\shell\Open
将“%1”附加到默认键值并重命名命令键。
关键是,

HKEY_CLASSES_ROOT\Word.Document.12\shell\Open\Command
HKEY_CLASSES_ROOT\Word.Document.12\shell\Open
重命名DDEXEC密钥

问候,