none
Run a Macro in a Ribbon RRS feed

  • Question

  • I am developing a ribbon add-in for Word 2007 and it is just a series of buttons that run Macros. This is the code for the first event handler. 

    Microsoft.Office.Interop.Word.

    Application oWord;

    Microsoft.Office.Interop.Word.

    _Document oDoc;

    Microsoft.Office.Interop.Word.

    Documents oDocs;

     

    object oMissing = System.Reflection.Missing.Value;

     

    private void btnOpenTemplate_Click(object sender, RibbonControlEventArgs e)

    {

     

    oWord =

    new Microsoft.Office.Interop.Word.Application();

    oWord.Visible =

    true;

    oDocs = oWord.Documents;

     

    object oFile = "c:\\MyWorksheet.dot";

    oDoc = oDocs.Open(

    ref oFile, ref oMissing,

     

    ref oMissing, ref oMissing, ref oMissing, ref oMissing,

     

    ref oMissing, ref oMissing, ref oMissing, ref oMissing,

     

    ref oMissing, ref oMissing, ref oMissing, ref oMissing,

     

    ref oMissing, ref oMissing);

    RunMacro(oWord,

    new object[] { "AutoNewCreate" });

     

    }

     

    private void RunMacro(object oApp, object[] oRunArgs)

    {

    oApp.GetType().InvokeMember(

    "Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);

    }



    This method opens up the document and runs the Macro just fine. However, when I try calling RunMacro() in an event handler for another button to call another macro, nothing happens.

    I have also added those commands to my Quick Launch menu and they work fine but I need to them to be in the Ribbon. Also, when I try to run that same macro with the oWord.Run() or oWord.RunOld() commands, nothing seems to happen.

    Any ideas? Is there a way to just simply emulate the buttons I am pressing to run the macros in VS2008 ribbon designer?

    thanks,
    Igor

    Wednesday, October 14, 2009 7:47 PM

Answers

  • Hello igor92128,

    I've run your code in my side, but unfortunately I could not reproduce this issue, here are the steps I take, if there's any differences from yours, please point me out.

    1. Open Word 2007, Press Alt+ F11
    2. At Visual Basic Editor Select "Normal" in Project Browser.
    3. Select "Insert Module" at Insert menu.
    4. Input following code:

    Sub AutoNewCreate()
    MsgBox "vsto call"
    End Sub

    5. Close Word
    6. Start Visual Studio 2008 , Create a New Word 2007 Add-In project.
    7. Add a Ribbon Visual Designer into the project.
    8. Drag a button into the Ribbon.
    9. Implement your code into the project.
    10. Create a new Word 2007 document with the name "
    MyWorksheet.dot" at "C:"
    11. Press F5 to run the project.

    But the result is not the same as yours, I could successfully call the VBA macro, the next steps I suggest you is try to call your VBA macro directly in VBA like this:

    Sub AutoNewCreate()
    Application.Run ("VBACall")
    End Sub

    If this call succeed, you may need to double check your VSTO code. I've also notice that, you started a new WinWord instance in your code (oWord =
    new Microsoft.Office.Interop.Word.Application()), this is not a recommand practise, to retrieve the Word Application instance, please use Globals.ThisAddIn.Application.

    Mean while, if you could package your code and send it to me it would be a great help for us to make it done, or you could upload your project to SkyDrive(or wherever we can get it). Please contact me via v-timli at microsoft.com (please replace at to @)

    Looking forward to your feedback.

    Thanks.





    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact EMAIL GONE


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tim Li Friday, October 16, 2009 2:09 AM
    Thursday, October 15, 2009 3:38 AM
  • Hi Igor

    <<The reason why I am doing this is to use an old .DOT VBA project with Word 2007 and incorporate the macros in a ribbon so that they are easier to run. So the goal is to just have this ribbon add-on installed in Word and the user will click the buttons on the ribbon to run the old VBA macros while the .DOT file is open in Word 2007 (converting it to .dotx seems to break some features).>>

    Converting to dotx would remove all the macros. What happens if you convert to dotm?

    The simplest would certainly be to include the Ribbon within the document file, linking it directly to VBA callbacks.

    In either case, the code in your btnOpenTemplate_Click procedure bothers me. Can you explain why it should start a new instance of Word (oWord =new Microsoft.Office.Interop.Word.Application(); ). This is going to de-couple the code running in the original instance (where the button is clicked) from the new instance. Why would you not be opening the file in the same instance of the Word application?

        Word.Application oWord = Globals.ThisAddin.Application

    Cindy Meister, VSTO/Word MVP
    Thursday, October 15, 2009 4:25 PM
    Moderator

All replies

  • Hello igor92128,

    I've run your code in my side, but unfortunately I could not reproduce this issue, here are the steps I take, if there's any differences from yours, please point me out.

    1. Open Word 2007, Press Alt+ F11
    2. At Visual Basic Editor Select "Normal" in Project Browser.
    3. Select "Insert Module" at Insert menu.
    4. Input following code:

    Sub AutoNewCreate()
    MsgBox "vsto call"
    End Sub

    5. Close Word
    6. Start Visual Studio 2008 , Create a New Word 2007 Add-In project.
    7. Add a Ribbon Visual Designer into the project.
    8. Drag a button into the Ribbon.
    9. Implement your code into the project.
    10. Create a new Word 2007 document with the name "
    MyWorksheet.dot" at "C:"
    11. Press F5 to run the project.

    But the result is not the same as yours, I could successfully call the VBA macro, the next steps I suggest you is try to call your VBA macro directly in VBA like this:

    Sub AutoNewCreate()
    Application.Run ("VBACall")
    End Sub

    If this call succeed, you may need to double check your VSTO code. I've also notice that, you started a new WinWord instance in your code (oWord =
    new Microsoft.Office.Interop.Word.Application()), this is not a recommand practise, to retrieve the Word Application instance, please use Globals.ThisAddIn.Application.

    Mean while, if you could package your code and send it to me it would be a great help for us to make it done, or you could upload your project to SkyDrive(or wherever we can get it). Please contact me via v-timli at microsoft.com (please replace at to @)

    Looking forward to your feedback.

    Thanks.





    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact EMAIL GONE


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tim Li Friday, October 16, 2009 2:09 AM
    Thursday, October 15, 2009 3:38 AM
  • Hi Igor

    there's something I don't understand about your project. (And could you please use the "Insert Code block" button when you paste code into a message to make it more easily readable.)

    <<I am developing a ribbon add-in for Word 2007 and it is just a series of buttons that run Macros. This is the code for the first event handler. >>

    What "event handler" is this, exactly? A button in a WinForm project? You're starting a new instance of a Word application, opening documents...

    How is the Ribbon you have related to this (WinForm?) project? As far as I know, there's no way to link a Ribbon in a Word document or an Add-in to code residing outside the project that is responsible for that Ribbon customization. The code linked to the Ribbon must be either in the add-in or in the document (whatever contains the Ribbon).

    This is why I'm having problems understanding exactly how things are linked together and how they are supposed to work (and whether they can work).

    Cindy Meister, VSTO/Word MVP
    Thursday, October 15, 2009 6:30 AM
    Moderator
  • Tim/Cindy,

    I can run the first macro just fine but the problem occurs if you have two buttons on the Ribbon and their Click event handlers are written like below. You can even try calling the same macro in the 2nd button, and let me know if it works.

    btnOpenTemplate_Click() - see above post for actual code
    {
        // open application & document code
       // RunMacro("AutoNewCreate")
    }

    btnRun_Click()
    {
       // RunMacro("AutoNewCreate")
    }

    So try this, Tim:

    1. Drag a second button onto the ribbon.
    2. Code the click event handler like this:
    button2_Click
    {
         RunMacro(oWord, new object[] { "AutoNewCreate" });
    }

    And let me know if the #2 button runs the same Macro.


    Cindy,

    The reason why I am doing this is to use an old .DOT VBA project with Word 2007 and incorporate the macros in a ribbon so that they are easier to run. So the goal is to just have this ribbon add-on installed in Word and the user will click the buttons on the ribbon to run the old VBA macros while the .DOT file is open in Word 2007 (converting it to .dotx seems to break some features).

    Thanks,
    Igor
    Thursday, October 15, 2009 3:28 PM
  • Hi Igor

    <<The reason why I am doing this is to use an old .DOT VBA project with Word 2007 and incorporate the macros in a ribbon so that they are easier to run. So the goal is to just have this ribbon add-on installed in Word and the user will click the buttons on the ribbon to run the old VBA macros while the .DOT file is open in Word 2007 (converting it to .dotx seems to break some features).>>

    Converting to dotx would remove all the macros. What happens if you convert to dotm?

    The simplest would certainly be to include the Ribbon within the document file, linking it directly to VBA callbacks.

    In either case, the code in your btnOpenTemplate_Click procedure bothers me. Can you explain why it should start a new instance of Word (oWord =new Microsoft.Office.Interop.Word.Application(); ). This is going to de-couple the code running in the original instance (where the button is clicked) from the new instance. Why would you not be opening the file in the same instance of the Word application?

        Word.Application oWord = Globals.ThisAddin.Application

    Cindy Meister, VSTO/Word MVP
    Thursday, October 15, 2009 4:25 PM
    Moderator
  • Hi Cindy,

    Yes, changing my code to set the file to open in the same instance of the Word application solved the problem.  It looks like the other instance was not seeing the code in the original instance.

    Thank you,
    Igor 
    Thursday, October 15, 2009 5:00 PM