1

I am fairly new to C# and VBA coding so bear with me I don't know what is necessary information and what is not. Anyway, I am trying to create a VSTO add-in for Word using C# in Visual Studio that essentially has a few buttons that run VBA macros when clicked. I have followed searched for solutions, but most apply to excel and don't seem to exactly solve my issue, the closest I can find is this previous question (How-to: Run existing Word VBA Macros from C# Ribbon Addin) but the solution did not resolve my issue.

So I have the Macro in a document, that works as expected if I run it directly from the document (there are no arguments for the macro code). I have the buttons created and the add-in ribbon appears when I open an instance of Word. I also have the following methods in my addin.cs file:

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

and the following method as the call onAction for the one button (say Button1)

    public void OnButton1(Office.IRibbonControl control)
    {
        RunMacro(Globals.ThisAddIn.Application, new object[] {"Button1Macro"});
    }

Any help would be greatly appreciated. Thanks

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Ksmart
  • 15
  • 4
  • You say the macro is "in a document". This is the open, active document when the Ribbon button is clicked? What is happening, exactly, when the button is clicked? For example, is there an error? – Cindy Meister Feb 23 '19 at 17:26
  • @CindyMeister Yes, the Word document in which the macro (aka "Button1Macro") is the open, active document that I am clicking the Ribbon button from. When I click on this button, nothing happens at all. No error, loading, nothing. – Ksmart Feb 25 '19 at 15:45

0 Answers0