Trigger / Execute Macro in Outlook 2007 / 2010

Add-in Express™ Support Service
That's what is more important than anything else

Trigger / Execute Macro in Outlook 2007 / 2010
 
Muller Stephan




Posts: 50
Joined: 2010-03-11
Hi guys,
I am trying to execute an Outlook Macro from my Addin. I read on the forum that the "OnAction" of Office Buttons should be used for that. Since I need dynamic macro execution, I simply dynamically add a toolbar which should execute my macro. Unfortunately, that does not work. How can I execute a macro from my addin? I am attaching the code:

Microsoft.Office.Core.CommandBar commandBar;
        Microsoft.Office.Core.CommandBarButton firstButton;
        Microsoft.Office.Core.CommandBarButton secondButton;
        private void adxCommandBarButton1_Click(object sender)
        {
            
            
            try
            {
                commandBar = OutlookApp.ActiveExplorer().CommandBars["Test"];
            }
            catch (ArgumentException e)
            {
                // Toolbar named Test does not exist so we should create it.
            }

            if (commandBar == null)
            {
                // Add a commandbar named Test.
                commandBar = OutlookApp.ActiveExplorer().CommandBars.Add("Test", 1, Type.Missing, true);
            }

            try
            {
                // Add a button to the command bar and an event handler.
                firstButton = (Microsoft.Office.Core.CommandBarButton)commandBar.Controls.Add(1, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                firstButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
                firstButton.Caption = "button 1";
                firstButton.Tag = "button1";
                firstButton.OnAction = "testMakro";
                //firstButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);
                firstButton.Execute();
                // Add a second button to the command bar and an event handler.
                secondButton = (Microsoft.Office.Core.CommandBarButton)commandBar.Controls.Add(1, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                commandBar.Visible = true;
            }
            catch (ArgumentException e)
            {
                MessageBox.Show(e.Message);
            }
        }
Posted 23 May, 2012 03:44:23 Top
Eugene Astafiev


Guest


Hi Stephan,

Thank you for providing me with your code.

First of all, I have noticed that you don't release underlying COM objects properly. Please read more about this in the http://www.add-in-express.com/creating-addins-blog/2008/10/30/releasing-office-objects-net/ article on our technical blog.

It looks like you refer to the similar http://www.add-in-express.com/forum/read.php?FID=5&TID=7006 forum thread. Did you try to debug the code? Do you get any exceptions or error messages?

Are macros allowed to run? You can read more about this on the http://www.dbforums.com/microsoft-excel/1205997-commandbars-onaction-property-fails.html forum.
Posted 23 May, 2012 04:07:39 Top
Muller Stephan




Posts: 50
Joined: 2010-03-11
Hey Eugene,
thank you for your quick answer. The security settings were not the problem.

However the problem was that my Macro was not accessible because it was "ThisOutlookSession.TestMacro" not "TestMacro". When I created a new sub and called it just "TestMacro", the code worked.


So the solution is working, thank you!


Kind Regards
Stephan
Posted 28 May, 2012 09:32:07 Top
Eugene Astafiev


Guest


You are welcome, Stephan! ;-)
Posted 28 May, 2012 09:53:30 Top