none
How to use a COM add-in to Obtain Information for Multiple Accounts RRS feed

  • Question

  • Hello,

    I’m using Visual Studio 2010 Pro and Outlook 2010.

    I’m trying to get the code from this article to work: http://msdn.microsoft.com/en-us/library/ff868554.aspx

    When I put that code into the ThisAddIn class of a Visual Studio Tools for Office add-in for Outlook 2010 and run it, it just opens Outlook and does not show anything.

    Can somebody explain how to get this code to show me the list of accounts in my Outlook profile?

    Any help would be gratefully appreciated.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, May 1, 2012 1:39 PM

Answers

  • Are you saying that calls into your addin are working now?
     
    If not, make sure you're following the model from my code and make sure that "ImportData" is the exported name of the method in the external interface.

    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:fb36fcc3-3a57-4c81-b931-be58ec1198f9...

    Ken,

    I finally did get your add-in to work by creating a clickonce and installing the add-in locally.  I used your VBA code to execute it from Outlook.

    I used this VBA code to execute the other add-in that i created which is from this link: http://msdn.microsoft.com/en-us/library/bb608614.aspx

    Sub CallVSTOMethod()
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("ExcelImportData")
        Set automationObject = addIn.Object
        automationObject.ImportData
    End Sub

    Thanks for all of your help.

    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    Ken Slovak MVP - Outlook
    Friday, May 11, 2012 8:35 PM
    Moderator

All replies

  • So what happens when you step through that code?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, May 1, 2012 2:02 PM
  • Hello Dmitry,

    I can't get it to stop on any line of code.  If i set a breakpoint on the first line of code it never stops there and just opens Outlook.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, May 1, 2012 2:11 PM
  • Are any breakpoints hit in your code?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, May 1, 2012 4:33 PM
  • Hello Dmitry,

    If I use the Debug/Step Ito menu option, it never tops.  It just opens Outlook and sits there.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, May 1, 2012 8:27 PM
  • So the problem is that your add-in is never loaded, not that soem particular fucntion does not work?

    Did it run before?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, May 1, 2012 11:51 PM
  • Dmitry,

    No, It never ran before.  I just copied it to see if I could learn something about VBA for Outlook.

    If the add-in is never loaded, then why does Outlook start?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Wednesday, May 2, 2012 3:17 AM
  • Hi Carneno,

    The addin has a independent AppDomain for itself. So Outlook will not crash if add-in has any exceptions when it is loading. It you copy the snippet to the ThisAddIn.cs. It will work, however you need see result in Visual Studio's Output window.

    Best Regards,

    T.X.

    Wednesday, May 2, 2012 4:16 AM
  • Why wouldn't Outlook run? It does not depend on your add-in in any way.

    Does the add-in get registered correctly? Do you see all relevant registry entries?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, May 2, 2012 7:16 AM
  • Hello T.X.

    I tried what you said.  I don't see anything in the Output window.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Wednesday, May 2, 2012 4:46 PM
  • Dmitry,

    How do i check that?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Wednesday, May 2, 2012 4:47 PM
  • Is your add-in listed in Options | Addins in Outlook?

    Is your add-in listed in HKLM(or HKCU)\Software\Microsoft\Office\Outlook\Addins?

    Doe your dll get loaded if you look ta the Outlook.exe process with the Process Explorer form SysInternals?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, May 2, 2012 4:56 PM
  • Dmitry,

    The add-in appears in the Outlook Options/Add-Ins under the area of Inactive Application Add-ins.

    The add-in also appears in the HKCU\Software\Microsoft\Outlook\Add-ins registry entry.

    I do not see anything about DLLs' when I see Outlook in Process Explorer.  Not sure of what to look for there.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Wednesday, May 2, 2012 5:23 PM
  • What is the LoadBehavior value for your add-in? Change it to 3.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, May 2, 2012 5:53 PM
  • Dmitry,

    I have changed the LoadBehavior for my add-in to 3 in the registry.

    I'm still getting no output.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Wednesday, May 2, 2012 9:33 PM
  • But does it get loaded now?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, May 2, 2012 9:35 PM
  • Dmitry,

    I'm not sure of what you mean by "does it get loaded now?".

    When I do the same thing that i have been doing in VS2010, and look at Outlook\Options\AddIns, the add-in appears under the area of Active Application Add-ins.

    I'm getting the same lack of output that i always get.

    Thanks,
    Tony.


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Wednesday, May 2, 2012 9:44 PM
  • Do you now see dll in Process Explorer?

    Do your breakpoints get hit?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, May 2, 2012 10:31 PM
  • Dmitry,

    No DLLs appear under Outlook in Process Explorer.

    BreakPoints still not being hit.

    Still getting same lack of output.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Wednesday, May 2, 2012 10:52 PM
  • Hello T.X.

    When you tried this did Outlook execute?

    What version of Visual Studio and Outlook are you using?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, May 3, 2012 4:45 PM
  • But does Outlook now show your add-in as active and loaded?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, May 3, 2012 4:50 PM
  • Dmitry,

    When I do the same thing that i have been doing in VS2010, and look at Outlook\Options\AddIns, the add-in appears under the area of Active Application Add-ins.

    When I look at Outlook\Developer\COM Add-Ins, the add-in appears with a check mark.

    I'm getting the same lack of output that i always get.

    Thanks,
    Tony.


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    • Edited by Carneno Thursday, May 3, 2012 5:01 PM
    Thursday, May 3, 2012 4:59 PM
  • Try to remove all that new code. just leave a bare bones add-in. Do the breakpoints get hit in the OnConnection callback?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, May 3, 2012 5:18 PM
  • Dmitry,

    I removed all the code that i inserted and put a breakpoint on ThisAddIn_Startup procedure and it did stop there.  After adding back in my code, I did the same thing and it did stop there again.

    I guess I don't understand the Debug\Step Into option for debugging.  I thought that would stop on the first line of code in my project.  Apparently not.

    I still do not see any output.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, May 3, 2012 5:49 PM
  • When and how do you invoke the code that you copied from MSDN?

    Can you step through it?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, May 3, 2012 5:54 PM
  • Dmitry,

    No, I cannot step through it.  I put my code back in, put a breakpoint on every line of that code and also on the ThisAddIn_Startup procedure.  It does not stop anywhere in my added code, but it does stop on the ThisAddIn_Startup procedure.

    What do you mean by "Invoke the code"?

    Still no output from anywhere.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, May 3, 2012 6:07 PM
  • Dmitry,

    Additionally, here is my whole ThisAddIn.CS file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Diagnostics;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    
    namespace EnumerateAccountsCSharpAddIn
    {
        public partial class ThisAddIn
        {
            private void EnumerateAccounts()
            {
                Outlook.Accounts accounts = Application.Session.Accounts;
    
                // Enumerate each account defined in the current profile. 
                foreach (Outlook.Account account in accounts)
                {
                    try
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("Account: " + account.DisplayName);
    
                        // If the account does not contain the SMTP address or 
                        // user name, use the AddressEntry and ExchangeUser objects. 
                        if (string.IsNullOrEmpty(account.SmtpAddress)
                            || string.IsNullOrEmpty(account.UserName))
                        {
                            Outlook.AddressEntry oAE =
                                account.CurrentUser.AddressEntry
                                as Outlook.AddressEntry;
    
                            // If the account is an Exchange account, 
                            // display also the Exchange server name and version. 
                            if (oAE.Type == "EX")
                            {
                                Outlook.ExchangeUser oEU =
                                    oAE.GetExchangeUser()
                                    as Outlook.ExchangeUser;
    
                                // Use ExchangeUser object to display user name 
                                // and SMTP address. 
                                sb.AppendLine("UserName: " +
                                    oEU.Name);
                                sb.AppendLine("SMTP: " +
                                    oEU.PrimarySmtpAddress);
    
                                // Use Account object to display the Exchange 
                                // server name and version information. 
                                sb.AppendLine("Exchange Server: " +
                                    account.ExchangeMailboxServerName);
                                sb.AppendLine("Exchange Server Version: " +
                                    account.ExchangeMailboxServerVersion);
                            }
                            // The account is not connected to an Exchange 
                            // Server, use the AddressEntry object to display only  
                            // the user name and SMTP address. 
                            else
                            {
                                sb.AppendLine("UserName: " +
                                    oAE.Name);
                                sb.AppendLine("SMTP: " +
                                    oAE.Address);
                            }
                        }
                        // The account contains SMTP address and 
                        // user name,  then the Account object is sufficient.  
                        else
                        {
                            sb.AppendLine("UserName: " +
                                account.UserName);
                            sb.AppendLine("SMTP: " +
                                account.SmtpAddress);
    
                            // If the account is an Exchange account, 
                            // display also the Exchange server name and version. 
                            if (account.AccountType ==
                                Outlook.OlAccountType.olExchange)
                            {
                                sb.AppendLine("Exchange Server: " +
                                    account.ExchangeMailboxServerName);
                                sb.AppendLine("Exchange Server Version: " +
                                    account.ExchangeMailboxServerVersion);
                            }
                        }
    
                        // If the account is connected to a delivery store, 
                        // display the store name as well. 
                        if (account.DeliveryStore != null)
                        {
                            sb.AppendLine("Delivery Store: " +
                                account.DeliveryStore.DisplayName);
                        }
                        sb.AppendLine("---------------------------------");
                        Debug.Write(sb.ToString());
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
            }
     
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }
    


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, May 3, 2012 6:09 PM
  • Dmitry,

    Also,I put all of my code in the ThisAddIn_Startup procedure.  It does stop on any line that I want, even the Debug.Write(sb.ToString()) line.

    But, I still do not see any output.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, May 3, 2012 6:26 PM
  • Why would it stop in the EnumerateAccounts function if you never invoke it?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, May 3, 2012 6:42 PM
  • Dmitry,

    Because it is in the ThisAddIn_Startup procedure?  I have no idea.

    What do you mean by "invoke it"?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, May 3, 2012 6:45 PM
  • Somebody or something must invoke your code. Outlook has no idea that you have a function called EnumerateAccounts.

    It does know that it needs to load you add-in and call a few methods on it (e.g. Startup). You need to call the EnumerateAccounts function from some othe rfunction that yo uknwo gets called, e.g. the Startup event event handler.

    If your code now runs, what do you expect the output to be? Your code simply writes some data to the Debug window. Is that not what you want?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, May 3, 2012 8:00 PM
  • Dmitry,

    OK.  So, what do i do?

    What does this have to do with Outlook?  Why does it execute Outlook?

    I don't see anything in the debug, output, immediate, error List or any other window.

    That's why i'm here.  I don't see any output and I don't know what this has to do with Outlook.

    Sorry, i'm just trying to learn something about creating VBA functions for Outlook.  I thought this would help me. The article where i got this is from the Outlook 2010 Developer Reference.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, May 3, 2012 8:18 PM
  • Debug.Write should output the specified value in the Output window in the Visual Studio IDE. What do you see in that window?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, May 3, 2012 8:27 PM
  • Absolutely nothing.


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, May 3, 2012 8:32 PM
  • Is it your intention to always output into the debug log?

    For all practical purposes, you can use MessageBox.Show() to display the data.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, May 3, 2012 8:41 PM
  • Hello Dimitry.

    Thanks for waking me up to displaying information using a MessageBox.Show method.

    Now that I have output, I’m faced with another issue with this add-in.

    After seeing some output, I decided to try to run the add-in from Outlook, which is what I wanted to do in the first place.

    I used the information from this article to see if I could get my add-in to run from Outlook: http://msdn.microsoft.com/en-us/library/bb608614.aspx#Y1112

    If I run the add-in from VS2010, it opens Outlook and I can execute the add-in using this macro:

    Sub CallVSTOMethod()
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("EnumerateAccountsCSharpAddIn")
        Set automationObject = addIn.Object
        automationObject.EnumerateAccounts
    End Sub
    

    I see the output as I should.

    If I close down my add-in project in VS2010 and open Outlook normally, when I run the macro, it gives this message:

    It gives this error after the line:    automationObject.EnumerateAccounts.

    Can you explain why this add-in runs fine when I execute it from VS2010, but gives an error when I try to call it from the Outlook macro?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Friday, May 4, 2012 4:07 PM
  • Does your add-in actually set the Object property of your add-in?

    What are you trying to do? Why does your VBA code need to invoke the add-in instead of just using the Outlook Object Model? 


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Friday, May 4, 2012 4:42 PM
  • Dmitry,

    Thanks for all of your help with this project.

    >> Does your add-in actually set the Object property of your add-in? <<

    How do I tell?

    I’m trying to get the same information by using a command from the outlook toolbar which calls the Outlook macro that I posted in my previous message.

    >> Why does your VBA code need to invoke the add-in instead of just using the Outlook Object Model? <<

    I have no idea.  What is the difference?

    I’m just trying to Call Code in an Application-Level Add-in from VBA.  The link that I posted in my previous message shows what I am trying to do.

    If you have another way of doing it, I would like to try it.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Friday, May 4, 2012 7:40 PM
  • You add-in needs to set the COMAddin.Object property to "this".

    I still do not understand why you need a COM add-in: if you are planning to use the code from a VBA script, why not simply convert the C# code that you found to VB script and skip the addin completely?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Friday, May 4, 2012 11:09 PM
  • Dmitry,

    Thanks for all of your help with this project.

    I have converted the C# code to VB and made some minor changes to make the VBA version run directly from Outlook VBA.  It runs and gives me the output.

    I stilll would like to know why the add-in version is not working.  Part of my reasoning for doing all this is to learn about the add-ins.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Monday, May 7, 2012 2:52 PM
  • It sure sounds like your add-in worked just fine, but you only had problems with Debug.Write()...


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Monday, May 7, 2012 2:57 PM
  • Dmitry,

    The problem is that I can't run the add-in from VBA.  I get this error:

    Thanks
    ,Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Monday, May 7, 2012 3:00 PM
  • That is because you addin-in did not set the AddIn.Object property

    Normally this is only used when VBA or some other add-in needs access to functionlaity not normally exposed through the Outlook Object Model, e.g. Extended MAPI - you can write an add-in in C++ or Delphi that exposes a set of fucntions to another add-in written in C# or VB.Net.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Monday, May 7, 2012 4:29 PM
  • Dmitry,

    I'm not understanding what you are saying.

    Can you help resolve this issue?

    I would like to complete the task of calling an add-in from my VBA in Outlook.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Monday, May 7, 2012 5:50 PM
  • When you add-in starts up, you need to set the Addin.Object property:

    Outlook.Addin addIn = Application.COMAddIns("EnumerateAccountsCSharpAddIn");

    addin.Object = this;

    

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Monday, May 7, 2012 8:15 PM
  • Dmitry,

    I'm getting the following error:

    Error 1 The type or namespace name 'Addin' does not exist in the namespace 'Microsoft.Office.Interop.Outlook' (are you missing an assembly reference?) 

    Where should I be putting those statements?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Monday, May 7, 2012 9:44 PM
  • I am sorry, COMAddin object actually is defined in MSO.dll along wiht other shared Office objects.
    

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Monday, May 7, 2012 10:56 PM
  • Dmitry,

    I can find that dll here: C:\Program Files\Common Files\microsoft\shared\OFFICE14\MSO.DLL, but I can't add it as a reference.  It just does nothing when I try to add it to the project references.

    Whan you say "you need to set the AddIn.Object property", are you talking about this code:

    Sub CallVSTOMethod()
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIn "EnumerateAccountsCSharpAddIn")
        Set automationObject = addIn.Object
        automationObject.EnumerateAccounts
    End Sub


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, May 8, 2012 1:05 PM
  • You need to add a reference to "Microsoft Office XX.Y Object Library" from the COM tab in the References dialog box in VS IDE.

    By setting the Addin.Object property, I mean your C# addin must set the Addin.Object property before is becomes accessible to yourVBA code. Again, I see no point doing that unless you want it as an exercise.

    

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, May 8, 2012 2:22 PM
  • Dmitry,

    Here is a screen shot of my references.  You can see that the Microsoft Office 14.0 Object Library is already there.  It will not allow me to add the MSO.DLL reference.  Even if I delete the reference that is there for Office.dll and add the MSO.DLL, it still puts in the Office.dll.

    When I look at other examples of how to call an add-in from VBA, it does not show what you are suggesting.  It only shows that in the VBA call.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    • Edited by Carneno Tuesday, May 8, 2012 3:28 PM
    Tuesday, May 8, 2012 3:23 PM
  • Tnen the declaration should be

    Microsoft.Office.CoreCOMAddin addin = ...

    Make sure you add the assembly to the uses clause.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, May 8, 2012 4:35 PM
  • Dmitry,

    This is not working.  There is no Microsoft.Office.CoreCOMAddin.

    Do you know how to call an office add-in from VBA?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, May 8, 2012 4:44 PM
  • BTW, if you are using VSTO (I don't use it), you will need to override the RequestComAddInAutomationService method - see http://msdn.microsoft.com/en-us/library/bb608614.aspx.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, May 8, 2012 4:48 PM
  • Microsoft.Office.Core.COMAddin
     
    COMAddin is an object in the Office object model.
     
    If you are using a shared addin you must do as Dmitry said to place your addin into the COMAddin.Object property: in the Connection class, in OnConnection() you set the addinInst property passed to you to "this":
     
    // At class level

    private Office.COMAddIn _addInInstance = null;

     

    // In OnConnection

    _addInInstance = (Office.COMAddIn)addInInst;

    _addInInstance.GetType().InvokeMember(Properties.Resources.Object,

    BindingFlags.Public | BindingFlags.SetProperty, null, addInInst, new object[] { this });

    After you do that any publicly exposed method or property in the public Connect class can be called from VBA code or elsewhere external to the addin.
     
    For VBA, if your addin is "Foobar.Connect" you would use something like this to call a method named "MyMethod" :
     
    Dim addin As Office.COMAddIn
    Set addin = Application.COMAddIns.Item("Foobar.Connect")
     
    Call addin.Object.MyMethod()
     
    If you are using VSTO, you must set up to call things as Dmitry indicated with an override for the VSTO RequestService() callbacks.

    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:0ebe3f66-8782-4328-bdfa-7e27f5cd2f68...

    Dmitry,

    This is not working.  There is no Microsoft.Office.CoreCOMAddin.

    Do you know how to call an office add-in from VBA?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    Ken Slovak MVP - Outlook
    Tuesday, May 8, 2012 5:04 PM
    Moderator
  • Dmitry,

    I already tried the project from the link that you supplied.

    That gives me the same error.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, May 8, 2012 6:34 PM
  • Ken,

    Thanks for your reply.

    I have no idea of what you are saying.

    There is no Connection Class in my project.

    Can you make the add-in work using the coding from this link?, that Dmitry suggested:

    http://msdn.microsoft.com/en-us/library/bb608614.aspx

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, May 8, 2012 6:44 PM
  • That article is written for use with a VSTO addin, is that what you're using? That's the first thing to establish. If so, you must follow the steps in the link Dmitry provided, and that you referenced.
     
    I've done that sort of thing many times with VSTO addins where it was required to expose methods or properties in my addins to outside code. See if how to do it is clearer to you if you look at the related code in one of the VSTO templates I created for my Outlook 2007 programming book. The link to the template downloads is at http://www.slovaktech.com/outlook_2007_templates.htm

    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:bb758aaa-32a2-4695-bd8d-4108fd63fb53...

    Ken,

    Thanks for your reply.

    I have no idea of what you are saying.

    There is no Connection Class in my project.

    Can you make the add-in work using the coding from this link?, that Dmitry suggested:

    http://msdn.microsoft.com/en-us/library/bb608614.aspx

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    Ken Slovak MVP - Outlook
    Tuesday, May 8, 2012 7:02 PM
    Moderator
  • Did you make sure RequestComAddInAutomationService gets called? Do you return a COM-visible object?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, May 8, 2012 7:07 PM
  • I did it exactly as it is in the article you linked me to.

    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, May 8, 2012 7:21 PM
  • Ken,

    I'm trying to do exactly what is outlined in the article pointed to.

    I'm using VS 2010 Pro and Office 2010.

    I coded that exactly the way it is presented.

    I would try the coding that you and Dmitry supplied but, I still don't know where to put that code and it won't allow me to without errors.

    Are you saying that I need to add code to that code?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    • Edited by Carneno Tuesday, May 8, 2012 7:57 PM
    Tuesday, May 8, 2012 7:57 PM
  • Ken,

    I get a Page Not Found when i try the download link that you provided.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, May 8, 2012 8:01 PM
  • You don't add that code when using VSTO.

    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:d7cdce65-83a0-40d2-b396-abe99ed2c2b1...

    Ken,

    I'm trying to do exactly what is outlined in the article pointed to.

    I'm using VS 2010 Pro and Office 2010.

    I coded that exactly the way it is presented.

    I would try the coding that you and Dmitry supplied but, I still don't know where to put that code and it won't allow me to without errors.

    Are you saying that I need to add code to that code?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.



    Ken Slovak MVP - Outlook
    Tuesday, May 8, 2012 9:56 PM
    Moderator
  • I just tried it and it worked for me again.
     
    Also, you'll get the same answers in your post in the VSTO forum, please don't multipost.

    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:9419a362-6521-4bd6-9e4a-52b65dbd2bec...

    Ken,

    I get a Page Not Found when i try the download link that you provided.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    Ken Slovak MVP - Outlook
    Tuesday, May 8, 2012 9:56 PM
    Moderator
  • OK.  So, if you did your project exactly the same way that is in the link provided, then I guess I don't know what I am doing.

    I won't be asking anymore questions since it works for you which most likely means that I don't know how to follow instructions.

    Thanks for all of suggestions.

    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, May 8, 2012 10:09 PM
  • Did you at least try the project from my Web site to see if it works for you?

    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:8157afb1-e465-4a58-9d60-8ed13dd1fce8...

    OK.  So, if you did your project exactly the same way that is in the link provided, then I guess I don't know what I am doing.

    I won't be asking anymore questions since it works for you which most likely means that I don't know how to follow instructions.

    Thanks for all of suggestions.

    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    Ken Slovak MVP - Outlook
    Wednesday, May 9, 2012 4:06 PM
    Moderator
  • Ken,

    I'm sorry, but I have to apologize.  My last reply to you was for something that i misunderstod about what you said.

    I still get Page Not Found when i try your link.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Wednesday, May 9, 2012 5:05 PM
  • You get a 404 from http://www.slovaktech.com/outlook_2007_templates.htm? I just tried it again and I had no problem reaching that page. I'm not sure what's up with that.

    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:64dc8f4f-27fa-493d-8d50-d48a590cb0d1...

    Ken,

    I'm sorry, but I have to apologize.  My last reply to you was for something that i misunderstod about what you said.

    I still get Page Not Found when i try your link.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    Ken Slovak MVP - Outlook
    Wednesday, May 9, 2012 5:25 PM
    Moderator
  • Ken,

    I experience a lot of weird, quirky, oddball things on the MSDN forums.  One of them is when i hover over the link that you posted, it shows this in the status bar of IE: http:/social.microsoft.com/Forums/en-us/outlookdev/thread.  I had to copy and paste the text of that link into the address bar and got to your site that way

    I downloaded and opened your VB.NET Shared Addin Project.  What is it supposed to do besides open Outlook?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Wednesday, May 9, 2012 7:24 PM
  • The VSTO projects are set up to be able to be called from outside the addin. They (C# and VB.NET ones) have working code to expose a method in the addin.

    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:eac4c969-6378-4d93-ab08-2a85cbeea519...

    Ken,

    I experience a lot of weird, quirky, oddball things on the MSDN forums.  One of them is when i hover over the link that you posted, it shows this in the status bar of IE: http:/social.microsoft.com/Forums/en-us/outlookdev/thread.  I had to copy and paste the text of that link into the address bar and got to your site that way

    I downloaded and opened your VB.NET Shared Addin Project.  What is it supposed to do besides open Outlook?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    Ken Slovak MVP - Outlook
    Wednesday, May 9, 2012 8:15 PM
    Moderator
  • OK.  I'm trying to use the VBA call method as shown in the link and shown here.

    Will this code work to call your methods?

    Sub CallVSTOMethod()
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("ExcelImportData")
        Set automationObject = addIn.Object
        automationObject.ImportData
    End Sub

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    • Edited by Carneno Wednesday, May 9, 2012 10:18 PM
    Wednesday, May 9, 2012 10:17 PM
  • At least for my addin template (let's say the VB.NET one), I'd do it something like this:
     
    Sub CallVSTOMethod()
        Dim addIn As Office.COMAddIn
        Set addIn = Application.COMAddIns("VSTO_VBOutlookTemplate")
        addIn.Object.CalledFromOutside
    End Sub


    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:c7eaf2e7-4321-4f5c-bb8e-b484b4783e77...

    OK.  I'm trying to use the VBA call method as shown in the link and shown here.

    Will this code work to call your methods?

    Sub CallVSTOMethod()
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("ExcelImportData")
        Set automationObject = addIn.Object
        automationObject.ImportData
    End Sub

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.



    Ken Slovak MVP - Outlook
    Thursday, May 10, 2012 2:09 PM
    Moderator
  • Ken,

    I'm getting this error on the Set line:

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, May 10, 2012 3:45 PM
  • OK. I just created a brand new VSTO addin that can be called from outside.
     
    Here is the Outlook VBA code that calls the addin method:
     
    Sub CallAddin()
      Dim oAddin As Office.COMAddIn
      Set oAddin = Application.COMAddins.Item("OutlookAddin1")
      oAddin.Object.CalledFromOutside
      Set oAddin = Nothing
    End Sub
     
    Here is the complete addin code I used. About all it does is respond to the outside call, but it illustrates the concept.
     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Xml;

    using Outlook = Microsoft.Office.Interop.Outlook;

    using Office = Microsoft.Office.Core;

    using System.Windows.Forms;

    using System.Reflection;

    using System.Runtime.InteropServices;

    namespace OutlookAddIn1

    {

    public partial class ThisAddIn

    {

    private Outlook.Application m_Outlook = null;

    private Outlook.NameSpace m_NameSpace = null;

    private string m_ProgID = "";

    private void ThisAddIn_Startup(object sender, System.EventArgs e)

    {

    try

    {

    // get the ProgID of the addin to use later with buttons/toolbars

    AssemblyName thisAssemblyName = null;

    thisAssemblyName = Assembly.GetExecutingAssembly().GetName();

    m_ProgID = thisAssemblyName.Name; //ProgID for the addin

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    try

    {

    AddinObject = new AutomationObject(this, m_ProgID);

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    m_Outlook = this.Application;

    try

    {

    m_NameSpace = m_Outlook.GetNamespace("MAPI");

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

    {

    }

    #region VSTO generated code

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InternalStartup()

    {

    this.Startup += new System.EventHandler(ThisAddIn_Startup);

    this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

    }

    #endregion

    public static AutomationObject AddinObject = null;

    private AddinUtilities addinUtilities;

    protected override object RequestComAddInAutomationService()

    {

    if (addinUtilities == null)

    {

    addinUtilities = new AddinUtilities();

    }

    return addinUtilities;

    }

    protected override object RequestService(Guid serviceGuid)

    {

    return base.RequestService(serviceGuid);

    }

    }

    public class AutomationObject

    {

    private Outlook.Application m_application;

    private string m_ProgID;

    // class constructor

    public AutomationObject(ThisAddIn application, string ProgID)

    {

    m_application = application.Application;

    m_ProgID = ProgID;

    }

    public string ProgID()

    {

    return m_ProgID;

    }

    public string ExplCount()

    {

    string RetVal = m_application.Explorers.Count.ToString();

    return RetVal;

    }

    public Outlook.Application App()

    {

    return m_application;

    }

    }

     

    [ComVisible(true)]

    [InterfaceType(ComInterfaceType.InterfaceIsDual)]

    public interface IAddinUtilities

    {

    void CalledFromOutside();

    }

    [ComVisible(true)]

    [ClassInterface(ClassInterfaceType.None)]

    public class AddinUtilities : IAddinUtilities

    {

    // Demonstrates a method that can be called from outside the addin.

    // This technique can be used to call functions and to read/write properties.

    public void CalledFromOutside()

    {

    AutomationObject myAddinObject = ThisAddIn.AddinObject;

    string Count = myAddinObject.ExplCount();

    string ID = myAddinObject.ProgID();

    //MessageBox.Show("This is a test of an outside call to the COM addin");

    MessageBox.Show("There are currently " + Count + " Explorers open.");

    }

    }

    }


    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:bd0be0e6-6332-4acb-ae1a-d3a716be6c36...

    Ken,

    I'm getting this error on the Set line:

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    Ken Slovak MVP - Outlook
    Thursday, May 10, 2012 6:05 PM
    Moderator
  • Ken,

    This works great, but only if I run it from VS2010.

    If I open Outlook from Start/Programs and run the VBA code, I get this error:

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, May 10, 2012 7:46 PM
  • If you want to run that addin without running in debug mode from VS you need to make a setup or ClickOnce project and install the addin code.

    I tested the code I just showed you both in debug mode from VS as well as from an installed setup.msi running in Outlook without VS running. It worked the same in both instances.

    My development and test environments in this case were VS 2010 and Outlook 2007 running on WinXP.


    Ken Slovak MVP - Outlook

    Thursday, May 10, 2012 8:26 PM
    Moderator
  • Ken,

    I finally did get your add-in to work by creating a clickonce and installing the add-in locally.  I used your VBA code to execute it from Outlook.

    I used this VBA code to execute the other add-in that i created which is from this link: http://msdn.microsoft.com/en-us/library/bb608614.aspx

    Sub CallVSTOMethod()
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("ExcelImportData")
        Set automationObject = addIn.Object
        automationObject.ImportData
    End Sub

    Thanks for all of your help.

    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Friday, May 11, 2012 8:16 PM
  • Are you saying that calls into your addin are working now?
     
    If not, make sure you're following the model from my code and make sure that "ImportData" is the exported name of the method in the external interface.

    --
    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
     
     
    "Carneno" <=?utf-8?B?Q2FybmVubw==?=> wrote in message news:fb36fcc3-3a57-4c81-b931-be58ec1198f9...

    Ken,

    I finally did get your add-in to work by creating a clickonce and installing the add-in locally.  I used your VBA code to execute it from Outlook.

    I used this VBA code to execute the other add-in that i created which is from this link: http://msdn.microsoft.com/en-us/library/bb608614.aspx

    Sub CallVSTOMethod()
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("ExcelImportData")
        Set automationObject = addIn.Object
        automationObject.ImportData
    End Sub

    Thanks for all of your help.

    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    Ken Slovak MVP - Outlook
    Friday, May 11, 2012 8:35 PM
    Moderator