2
votes

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

3
"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)." Run doesn't require it; it may be a peculiarity of the way .NET interfaces with COM/VBA that you're required by VB.NET to fill in all parameters, optional or not.Steve Rindsberg
do you see any error at application.Run?Brijesh Mishra
No there is no error at all but nothing happens. I tried to directly call the routine : Globals.ThisAddIn.Application.Run("PPspliT_main", null); but still nothing. In fact if you know another way to use this VBA code within my C# project It would be ok since I have the code of the PPspliT add-in and it's pptm file as well.Acacio Martins

3 Answers

2
votes

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 !

0
votes

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);
0
votes

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.