none
Error - Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))

    Question

  • Hi, i am triying to make an outlook addin to call a macro i have in the outlook vba editor.

     

    The code i have is as follows        

     

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
               
                Outlook.Application app = null;
               
                if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
                {
                    MessageBox.Show("Hello there is at least 1 instance");
                    app = (Microsoft.Office.Interop.Outlook.ApplicationClass)(Marshal.GetActiveObject("Outlook.Application"));
                    MessageBox.Show(app.Application.Name.ToString());
                }
                else
                {
                    MessageBox.Show("Sorry there are 0 instances");
                    app = new Outlook.Application();
                    Outlook.NameSpace nameSpace = app.GetNamespace("MAPI");
                    nameSpace.Logon("", "", Missing.Value, Missing.Value);
                    nameSpace = null;
                }

                RunMacro(app, new Object[] { "showFormDisplay" });
               
               
            }
          
            private void RunMacro(object oApp, object[] oRunArgs)
            {
               try
               {
                   
                    oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
               }

     

    With the line in bold being the one that always throws an exception. Does anyone have any ideas?

     

    Thanks

     

    • Moved by SamAgain Monday, November 15, 2010 9:44 AM better fit (From:.NET Base Class Library)
    Friday, November 12, 2010 9:35 AM

Answers

  • That is correct.
     
    Although you can call a macro from your addin, you are better off not relying on the macro being there and putting the code for it into your addin. In addition, although calling a macro will work you do need to remember that it is not supported by MS and there are no guarantees it will continue to work in the future.
     
    If your macro name is "Foobar" it would need to be in the ThisOutlookSession class module to be able to be called from outside. In VBA terms it would be called as Outlook.Application.Foobar(). In c# you would get the Application object and just call the method "Foobar".
     
           private void RunMacro(object oApp, object[] oRunArgs)
            {
               try
               {
                   
                    oApp.GetType().InvokeMember("Foobar", System.Reflection.BindingFlags.Public |     
                        System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
               }

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "jermaindefoe" <=?utf-8?B?amVybWFpbmRlZm9l?=> wrote in message news:c2f9391d-3330-4e43-b2f2-8056c1ab4c0b...
    i think i am having problems because the outlook object has no run method, hence the unknown name error?

    Ken Slovak MVP - Outlook
    • Marked as answer by Bruce Song Friday, November 19, 2010 11:54 AM
    Monday, November 15, 2010 2:27 PM
    Moderator

All replies

  • i think i am having problems because the outlook object has no run method, hence the unknown name error?
    Monday, November 15, 2010 10:07 AM
  • That is correct.
     
    Although you can call a macro from your addin, you are better off not relying on the macro being there and putting the code for it into your addin. In addition, although calling a macro will work you do need to remember that it is not supported by MS and there are no guarantees it will continue to work in the future.
     
    If your macro name is "Foobar" it would need to be in the ThisOutlookSession class module to be able to be called from outside. In VBA terms it would be called as Outlook.Application.Foobar(). In c# you would get the Application object and just call the method "Foobar".
     
           private void RunMacro(object oApp, object[] oRunArgs)
            {
               try
               {
                   
                    oApp.GetType().InvokeMember("Foobar", System.Reflection.BindingFlags.Public |     
                        System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
               }

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "jermaindefoe" <=?utf-8?B?amVybWFpbmRlZm9l?=> wrote in message news:c2f9391d-3330-4e43-b2f2-8056c1ab4c0b...
    i think i am having problems because the outlook object has no run method, hence the unknown name error?

    Ken Slovak MVP - Outlook
    • Marked as answer by Bruce Song Friday, November 19, 2010 11:54 AM
    Monday, November 15, 2010 2:27 PM
    Moderator
  • That is correct.
     
    Although you can call a macro from your addin, you are better off not relying on the macro being there and putting the code for it into your addin. In addition, although calling a macro will work you do need to remember that it is not supported by MS and there are no guarantees it will continue to work in the future.
     
    If your macro name is "Foobar" it would need to be in the ThisOutlookSession class module to be able to be called from outside. In VBA terms it would be called as Outlook.Application.Foobar(). In c# you would get the Application object and just call the method "Foobar".
     
           private void RunMacro(object oApp, object[] oRunArgs)
            {
               try
               {
                   
                    oApp.GetType().InvokeMember("Foobar", System.Reflection.BindingFlags .Public |      
                        System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
               }

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "jermaindefoe" <=?utf-8?B?amVybWFpbmRlZm9l?=> wrote in message news:c2f9391d-3330-4e43-b2f2-8056c1ab4c0b ...
    i think i am having problems because the outlook object has no run method, hence the unknown name error?

    Ken Slovak MVP - Outlook
    ok, thanks very much for the help. I was putting oRunArgs as the macro name. What should be in the orunargs array then if not the name of the macro name to run? Should it be the arguments the vba macro takes, as the macro i am running is a simple show form command, the method takes no parameters. Thanks again
    Monday, November 15, 2010 5:01 PM
  • The run arguments would be whatever calling arguments you need. If there are none you can define it like this:
     
            object[] oRunArgs = new Object[] { };
     
    In other words just as an empty object array.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "jermaindefoe" <=?utf-8?B?amVybWFpbmRlZm9l?=> wrote in message news:3d7a5c81-a41c-42ca-a195-cc505e18a058...
    <snip>
     
    ok, thanks very much for the help. I was putting oRunArgs as the macro name. What should be in the orunargs array then if not the name of the macro name to run? Should it be the arguments the vba macro takes, as the macro i am running is a simple show form command, the method takes no parameters. Thanks again

    Ken Slovak MVP - Outlook
    Monday, November 15, 2010 5:15 PM
    Moderator
  • could you please confirm whether or not the following code should work. The method i am calling is called showFormDisplay and lives in the thisOutlookSession and is defined as follows

     

    Sub showFormDisplay()
    UserForm3.Show
    End Sub

    This is the c# code i am using to call it. I still get the same "unknown name" error message, maybe becuase the sub needs to be defined as public or a function for it to be visible?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Diagnostics;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using Microsoft.Office.Core;
              
    namespace OutlookAddIn
    {
      [ComVisible(true)]
    
      public partial class ThisAddIn
      {
        
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
          Microsoft.Office.Interop.Outlook.Application app = null;
          MessageBox.Show(sender.GetType().ToString());
          
    
          if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
          {
            MessageBox.Show("Hello there is at least 1 instance");
            app = (Microsoft.Office.Interop.Outlook.Application)(Marshal.GetActiveObject("Outlook.Application"));
     
            MessageBox.Show(app.Application.Name.ToString());       
           
          }
          RunMacro(app, new object[] {});
        }
       
        private void RunMacro(object oApp, object[] oRunArgs)
        {
          
          
          try
          {
            oApp.GetType().InvokeMember("showFormDisplay", System.Reflection.BindingFlags.Public |
              System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
          }
          catch (Exception e)
          {
            MessageBox.Show(e.ToString());
    
          }
        }
    
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
    
        }
        private void InternalStartup()
        {
          this.Startup += new System.EventHandler(ThisAddIn_Startup);
          this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
      }
    }

    Tuesday, November 16, 2010 9:12 AM
  • the above code is working, but it needs the visual basic editor open in outlook for it to run. Which is quite annyoing. Is there anyway round this?
    Tuesday, November 16, 2010 10:59 AM
  • Why are you using that code to get the Outlook.Application object in a VSTO addin? You are not only complicating things but you aren't using the trusted Application object that VSTO is handing you.
     
    A Sub declaration is global or public unless you declare it private in scope.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "jermaindefoe" <=?utf-8?B?amVybWFpbmRlZm9l?=> wrote in message news:7ca003b9-cc12-40df-9de0-8b35eceff66a...

    could you please confirm whether or not the following code should work. The method i am calling is called showFormDisplay and lives in the thisOutlookSession and is defined as follows

     

    Sub showFormDisplay()
    UserForm3.Show
    End Sub

    This is the c# code i am using to call it. I still get the same "unknown name" error message, maybe becuase the sub needs to be defined as public or a function for it to be visible?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Diagnostics;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using Microsoft.Office.Core;
         
    namespace OutlookAddIn
    {
     [ComVisible(true)]
    
     public partial class ThisAddIn
     {
      
      private void ThisAddIn_Startup(object sender, System.EventArgs e)
      {
       Microsoft.Office.Interop.Outlook.Application app = null;
       MessageBox.Show(sender.GetType().ToString());
       
    
       if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
       {
        MessageBox.Show("Hello there is at least 1 instance");
        app = (Microsoft.Office.Interop.Outlook.Application)(Marshal.GetActiveObject("Outlook.Application"));
     
        MessageBox.Show(app.Application.Name.ToString());    
        
       }
       RunMacro(app, new object[] {});
      }
      
      private void RunMacro(object oApp, object[] oRunArgs)
      {
       
       
       try
       {
        oApp.GetType().InvokeMember("showFormDisplay", System.Reflection.BindingFlags.Public |
         System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
       }
       catch (Exception e)
       {
        MessageBox.Show(e.ToString());
    
       }
      }
    
      private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
      {
    
      }
      private void InternalStartup()
      {
       this.Startup += new System.EventHandler(ThisAddIn_Startup);
       this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
      }
     }
    }


    Ken Slovak MVP - Outlook
    Tuesday, November 16, 2010 2:33 PM
    Moderator
  • You might need to initialize the VBA session by having some code in the Application_Startup() event in ThisOutlookSession. It might also be related to your security settings.
     
    As this is not supported at all I haven't played with it in many years.
     
    It's just so much easier to put the same macro code in an addin and not worry about macro security, deploying VBA code and all the other problems that it's just not worth it.
     
    You will have to experiment to get it working the way you want.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "jermaindefoe" <=?utf-8?B?amVybWFpbmRlZm9l?=> wrote in message news:bbfe9ea0-c5f4-4796-abb2-9a01c0b3c5dd...
    the above code is working, but it needs the visual basic editor open in outlook for it to run. Which is quite annyoing. Is there anyway round this?

    Ken Slovak MVP - Outlook
    Tuesday, November 16, 2010 2:34 PM
    Moderator