Call VBA AddIn macro from VSTO PowerPoint Ribbon

最后都变了- 提交于 2019-12-30 07:23:26

问题


I've been stuck for a few hours on this problem :

I am developing a PowerPoint AddIn in C# and I want to use a macro from another AddIn which is a PPAM file. The PPAM file is installed and enabled.

In the Application reference I found that I need to use the Application.Run method but I cannot get it working (nothing happens)... Here is my code:

Globals.ThisAddIn.Application.Run("PPspliT.ppam!PPspliT.PPspliT_main", null);

PPspliT.ppam is the installed AddIn (which is located here : C:\Users\XXXX\AppData\Roaming\Microsoft\AddIns\PPspliT\)

The module in which the PPspliT_main macro is called is named PPspliT.

Another thing I find strange is that Run needs to take two arguments even if the macro doesn't have any argument (that's why I put null as second argument).

I also tried to install the AddIn programmatically using this :

String addinPath = @"C:\Users\XXXXX\AppData\Roaming\Microsoft\AddIns\PPspliT";
var macroFilePath = Path.Combine(addinPath, "PPspliT.ppam");
var addins = Globals.ThisAddIn.Application.AddIns.Add(macroFilePath);
if (!(addins.Registered == MsoTriState.msoTrue && addins.Loaded == MsoTriState.msoTrue))
{
  addins.Registered = MsoTriState.msoTrue;
  addins.Loaded = MsoTriState.msoTrue;
}
var app = Globals.ThisAddIn.Application;
string macroToInvoke = string.Format("{0}!{1}", "PPspliT.ppam", "PPspliT.PPspliT_main");
Globals.ThisAddIn.Application.Run(macroToInvoke, null);

Thanks for your help! Acacio


回答1:


This thing was driving me crazy but I found how to get it working ! Here is what I did (using this http://support.microsoft.com/kb/306682 :

So as I explained in my question I first programmatically register and load the add-in then I do the following :

    private void RunMacro(object oApp, object[] oRunArgs)
    {
       oApp.GetType().InvokeMember("Run",
       System.Reflection.BindingFlags.Default |
       System.Reflection.BindingFlags.InvokeMethod,
       null, oApp, oRunArgs);
    }

Globals.ThisAddIn.RunMacro(Globals.ThisAddIn.Application , new object[] {"PPspliT_main"});

Thanks to everyone for your help !




回答2:


You should try opening the presentation-enabled macro (PPAM) before requesting it to run (via Application.Presentations.Open). The MSDN reference for Application.Run states that the presentation must be loaded (interpreted to mean previously opened).

Presentation ppam = Globals.ThisAddIn.Application.Presentations.Open(macroFilePath);
string macroToInvoke = string.Format("{0}!{1}", ppam.Name, "PPspliT.PPspliT_main");
Globals.ThisAddIn.Application.Run(macroToInvoke, null);



回答3:


Just pulled up a bit of code in VB6 that calls a routine in a loaded add-in

Assuming a reference to a running instance of PPT in oPPTApp and a Public subroutine in the loaded add-in:

Public Sub BlahBlah()
   MsgBox "Hah!  You found me!"
End Sub

This will launch it:

oPPTApp.Run "BlahBlah"

Note that you don't need to provide the name of the add-in or the name of the module where your subroutine lives.

Again, this is how it works from VB5/6 or other VBA routines.



来源:https://stackoverflow.com/questions/9261634/call-vba-addin-macro-from-vsto-powerpoint-ribbon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!