locked
Outlook 2007 Late Binding - Insert default signature RRS feed

  • Question

  • Hi I'm having a bit of trouble inserting a user's default signature using late binding.

    I found the following VB method on MSDN: 
    http://msdn.microsoft.com/en-us/library/dd492012(v=office.12).aspx#Outlook2007ProgrammingCh17_WorkingWithOutlookSignatures 

    Set objMsg = Application.CreateItem(olMailItem)
    Set objSigDoc = objMsg.GetInspector.WordEditor
    Set objSel = objSigDoc.Windows(1).Selection
    With objSel
        .Collapse wdCollapseStart
        .MoveEnd WdUnits.wdStory, 1
        .Copy
    End With

    I would like a late binding version of this.

    This is what my code looks like:

     public class OutlookEmailer
        {
            private object oApp;
            private object oNameSpace;
            private object oOutboxFolder;
           
            public OutlookEmailer()
            {
                Type outlook_app_type;
                object[] parameter = new object[1];
                outlook_app_type = Type.GetTypeFromProgID("Outlook.Application");
                oApp = Activator.CreateInstance(outlook_app_type);
                parameter[0] = "MAPI";
                oNameSpace = outlook_app_type.InvokeMember("GetNamespace", BindingFlags.InvokeMethod, null, oApp, parameter);
                var Logon_parameter = new object[4] { null, null, true, true };
                oNameSpace.GetType().InvokeMember("Logon", BindingFlags.InvokeMethod, null, oNameSpace, Logon_parameter);
                //            
                var GetDefaultFolder_parameter = new object[1] { 6 }; // 6 = olFolderInbox (olFolder Enum)
                oOutboxFolder = oNameSpace.GetType().InvokeMember("GetDefaultFolder", BindingFlags.InvokeMethod, null, oNameSpace, GetDefaultFolder_parameter);
            }
    
            // Create email using Outlook
            public void SendOutlookEmail(string toValue, string subjectValue, string bodyValue, string[] attachmentPaths)
            {
                // Create Mail Item
                var CreateItem_parameter = new object[1] { 0 }; // 0 = olMailItem (olItemType)
                object oMailItem = oApp.GetType().InvokeMember("CreateItem", BindingFlags.InvokeMethod, null, oApp, CreateItem_parameter);
                
                var mail_item_type = oMailItem.GetType();
                mail_item_type.InvokeMember("To", BindingFlags.SetProperty, null, oMailItem, new object[] { toValue });
                mail_item_type.InvokeMember("Subject", BindingFlags.SetProperty, null, oMailItem, new object[] { subjectValue });
                mail_item_type.InvokeMember("HTMLBody", BindingFlags.SetProperty, null, oMailItem, new object[] { bodyValue });
    
                //// Add default Signature
                ////**********************
                //var olEditorWord = new object[1] { 4 }; // 4 = olEditorWord (EditorType)
                //// Get the word Editor from curr mail Item
                //object oSigDoc = oMailItem.GetType().InvokeMember("GetInspector", BindingFlags.GetProperty, null, oMailItem, olEditorWord);
                //// Select the word editor window from the word editor - Selection.Windows(1) appears to be from Word.Document object !!! Cant use with Outlook !!!
                //var Windows_1 = new object[1] { ? }; // ? = Windows(1)
                //object objSel = oSigDoc.GetType().InvokeMember("Selection", BindingFlags.GetProperty, null, oSigDoc, Windows_1);
                //// Set properties for the wordEditor Selection
                //var objSelect_type = objSel.GetType();
                ////
                //var wdCollapseStart = new object[1] { ? }; // ? = wdCollapseStart
                //objSelect_type.InvokeMember("Collapse", BindingFlags.SetProperty, null, objSel, wdCollapseStart);
                //var wdStory = new object[1] { ? }; // ? = wdUnits.wdStory
                //objSelect_type.InvokeMember("Collapse", BindingFlags.SetProperty, null, objSel, wdStory);
    
                // Display mail message            
                mail_item_type.InvokeMember("Display", BindingFlags.InvokeMethod, null, oMailItem, new object[] { true });
            }
        }
    

    Can somebody Help me?

     




    • Edited by gits6 Friday, September 23, 2011 2:33 PM
    Friday, September 23, 2011 1:34 PM

Answers

  • The usual rule of thumb is to reference and have installed on your computer the earliest version of Outlook you plan to support. The PIA's are forward compatible, so using an older PIA for a later version of Outlook will work with some deprecated exceptions.
     
    However, there were no PIA's for 2000, they were created using modified 2002 PIA's. I generally try to avoid managed code for 2000/2002 as many things can be problematic. In most cases that works as the number of 2000/2002 installations is pretty small these days.
     
    If you do have to support all those versions I guess late binding is about as good a way as any, although performance will be down versus using a PIA.
     
    What you need to do first of all is to check the Outlook version and check Inspector.IsWordMail. In Outlook 2000/2002/2003 use of WordMail is optional and your code will fire exceptions if WordMail isn't being used. If the Outlook editor is being used there's no real way of selecting or copying the signature text other than creating a new email and reading the entire Body property of the MailItem (ActiveInspector().CurrentItem) if you are using the Outlook object model.
     
    A good place for you to start is at www.outlookcode.com, it has a lot of Outlook programming information and samples, and the forums there will usually have information related to what you need. Search at the site on "signatures" to see what's there.
     
    So what problem specifically are you having with getting the information with the code? What exceptions or whatever?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "gits6" <=?utf-8?B?Z2l0czY=?=> wrote in message news:63d3f775-774b-4807-9486-0b638b1a28b8...

    Hi Ken, Thanks for the response.

    I'm having trouble trying to reference the outlook users signature for an late bound outlook email object using only c#.

    The reason why I'm using late binding is because, I am trying to achieve cross version compatibility for MS Outlook. 2000-2010. Referencing the Outlook assemblies in C# via .NET would require my application to be specific to an version of outlook. This is also true for COM PIA assemblies. Making my application compatible for Outlook 2000-2010 would require me to reference all of those assemblies, which intern would increase the size of the issued application significantly.

    Unfortunately I do not know of any way other than late binding to achieve this. Currently I'm trying to use the MSDN documentation to feel my way trough the outlook object model. This is considerably more difficult than just browsing the object model through visual studio. Bear in mind that I'm a novice pertaining to office integration matters and would appreciate any tips you could throw my way.

    I would welcome any suggestions of achieving cross version compatibility other than referencing all of the office assemblies.


    Ken Slovak MVP - Outlook
    Friday, September 23, 2011 8:49 PM

All replies

  • What exactly is the problem, and why are you using late binding anyway? The example for VBScript has to use late binding, that's all there is in VBScript. That doesn't mean you can't use the object model with fully specified objects in your C# code.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "gits6" <=?utf-8?B?Z2l0czY=?=> wrote in message news:35409a68-2dab-4cb1-ba2b-746d9f4f4516...

    Hi I'm having a bit of trouble inserting a user's default signature using late binding.

    I found the following VB method on MSDN: 
    http://msdn.microsoft.com/en-us/library/dd492012(v=office.12).aspx#Outlook2007ProgrammingCh17_WorkingWithOutlookSignatures 

    Set objMsg = Application.CreateItem(olMailItem)
    Set objSigDoc = objMsg.GetInspector.WordEditor
    Set objSel = objSigDoc.Windows(1).Selection
    With objSel
        .Collapse wdCollapseStart
        .MoveEnd WdUnits.wdStory, 1
        .Copy
    End With

    I would like a late binding version of this.

    This is what my code looks like:

     public class OutlookEmailer
        {
            private object oApp;
            private object oNameSpace;
            private object oOutboxFolder;
           
            public OutlookEmailer()
            {
                Type outlook_app_type;
                object[] parameter = new object[1];
                outlook_app_type = Type.GetTypeFromProgID("Outlook.Application");
                oApp = Activator.CreateInstance(outlook_app_type);
                parameter[0] = "MAPI";
                oNameSpace = outlook_app_type.InvokeMember("GetNamespace", BindingFlags.InvokeMethod, null, oApp, parameter);
                var Logon_parameter = new object[4] { null, null, true, true };
                oNameSpace.GetType().InvokeMember("Logon", BindingFlags.InvokeMethod, null, oNameSpace, Logon_parameter);
                //            
                var GetDefaultFolder_parameter = new object[1] { 6 }; // 6 = olFolderInbox (olFolder Enum)
                oOutboxFolder = oNameSpace.GetType().InvokeMember("GetDefaultFolder", BindingFlags.InvokeMethod, null, oNameSpace, GetDefaultFolder_parameter);
            }
    
            // Create email using Outlook
            public void SendOutlookEmail(string toValue, string subjectValue, string bodyValue, string[] attachmentPaths)
            {
                // Create Mail Item
                var CreateItem_parameter = new object[1] { 0 }; // 0 = olMailItem (olItemType)
                object oMailItem = oApp.GetType().InvokeMember("CreateItem", BindingFlags.InvokeMethod, null, oApp, CreateItem_parameter);
                
                var mail_item_type = oMailItem.GetType();
                mail_item_type.InvokeMember("To", BindingFlags.SetProperty, null, oMailItem, new object[] { toValue });
                mail_item_type.InvokeMember("Subject", BindingFlags.SetProperty, null, oMailItem, new object[] { subjectValue });
                mail_item_type.InvokeMember("HTMLBody", BindingFlags.SetProperty, null, oMailItem, new object[] { bodyValue });
    
                //// Add default Signature
                ////**********************
                //var olEditorWord = new object[1] { 4 }; // 4 = olEditorWord (EditorType)
                //// Get the word Editor from curr mail Item
                //object oSigDoc = oMailItem.GetType().InvokeMember("GetInspector", BindingFlags.GetProperty, null, oMailItem, olEditorWord);
                //// Select the word editor window from the word editor - Selection.Windows(1) appears to be from Word.Document object !!! Cant use with Outlook !!!
                //var Windows_1 = new object[1] { ? }; // ? = Windows(1)
                //object objSel = oSigDoc.GetType().InvokeMember("Selection", BindingFlags.GetProperty, null, oSigDoc, Windows_1);
                //// Set properties for the wordEditor Selection
                //var objSelect_type = objSel.GetType();
                ////
                //var wdCollapseStart = new object[1] { ? }; // ? = wdCollapseStart
                //objSelect_type.InvokeMember("Collapse", BindingFlags.SetProperty, null, objSel, wdCollapseStart);
                //var wdStory = new object[1] { ? }; // ? = wdUnits.wdStory
                //objSelect_type.InvokeMember("Collapse", BindingFlags.SetProperty, null, objSel, wdStory);
    
                // Display mail message            
                mail_item_type.InvokeMember("Display", BindingFlags.InvokeMethod, null, oMailItem, new object[] { true });
            }
        }
    

    Can somebody Help me?

     





    Ken Slovak MVP - Outlook
    Friday, September 23, 2011 5:59 PM
  • Hi Ken, Thanks for the response.

    I'm having trouble trying to reference the outlook users signature for an late bound outlook email object using only c#.

    The reason why I'm using late binding is because, I am trying to achieve cross version compatibility for MS Outlook. 2000-2010. Referencing the Outlook assemblies in C# via .NET would require my application to be specific to an version of outlook. This is also true for COM PIA assemblies. Making my application compatible for Outlook 2000-2010 would require me to reference all of those assemblies, which intern would increase the size of the issued application significantly.

    Unfortunately I do not know of any way other than late binding to achieve this. Currently I'm trying to use the MSDN documentation to feel my way trough the outlook object model. This is considerably more difficult than just browsing the object model through visual studio. Bear in mind that I'm a novice pertaining to office integration matters and would appreciate any tips you could throw my way.

    I would welcome any suggestions of achieving cross version compatibility other than referencing all of the office assemblies.

    Friday, September 23, 2011 7:30 PM
  • Hi Ken, Thanks for the response.

    I'm having trouble trying to reference the outlook users signature for an late bound outlook email object using only c#.

    The reason why I'm using late binding is because, I am trying to achieve cross version compatibility for MS Outlook. 2000-2010. Referencing the Outlook assemblies in C# via .NET would require my application to be specific to an version of outlook. This is also true for COM PIA assemblies. Making my application compatible for Outlook 2000-2010 would require me to reference all of those assemblies, which intern would increase the size of the issued application significantly.

    Unfortunately I do not know of any way other than late binding to achieve this. Currently I'm trying to use the MSDN documentation to feel my way trough the outlook object model. This is considerably more difficult than just browsing the object model through visual studio. Bear in mind that I'm a novice pertaining to office integration matters and would appreciate any tips you could throw my way.

    I would welcome any suggestions of achieving cross version compatibility other than referencing all of the office assemblies.

    You should do that in this order:

    - create mail

    - get inspector

    - get mail body (signature is in body)

    - merge your bodyvalue with mail body

    - set mail body

    - display inspector

    Friday, September 23, 2011 8:35 PM
  • The usual rule of thumb is to reference and have installed on your computer the earliest version of Outlook you plan to support. The PIA's are forward compatible, so using an older PIA for a later version of Outlook will work with some deprecated exceptions.
     
    However, there were no PIA's for 2000, they were created using modified 2002 PIA's. I generally try to avoid managed code for 2000/2002 as many things can be problematic. In most cases that works as the number of 2000/2002 installations is pretty small these days.
     
    If you do have to support all those versions I guess late binding is about as good a way as any, although performance will be down versus using a PIA.
     
    What you need to do first of all is to check the Outlook version and check Inspector.IsWordMail. In Outlook 2000/2002/2003 use of WordMail is optional and your code will fire exceptions if WordMail isn't being used. If the Outlook editor is being used there's no real way of selecting or copying the signature text other than creating a new email and reading the entire Body property of the MailItem (ActiveInspector().CurrentItem) if you are using the Outlook object model.
     
    A good place for you to start is at www.outlookcode.com, it has a lot of Outlook programming information and samples, and the forums there will usually have information related to what you need. Search at the site on "signatures" to see what's there.
     
    So what problem specifically are you having with getting the information with the code? What exceptions or whatever?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "gits6" <=?utf-8?B?Z2l0czY=?=> wrote in message news:63d3f775-774b-4807-9486-0b638b1a28b8...

    Hi Ken, Thanks for the response.

    I'm having trouble trying to reference the outlook users signature for an late bound outlook email object using only c#.

    The reason why I'm using late binding is because, I am trying to achieve cross version compatibility for MS Outlook. 2000-2010. Referencing the Outlook assemblies in C# via .NET would require my application to be specific to an version of outlook. This is also true for COM PIA assemblies. Making my application compatible for Outlook 2000-2010 would require me to reference all of those assemblies, which intern would increase the size of the issued application significantly.

    Unfortunately I do not know of any way other than late binding to achieve this. Currently I'm trying to use the MSDN documentation to feel my way trough the outlook object model. This is considerably more difficult than just browsing the object model through visual studio. Bear in mind that I'm a novice pertaining to office integration matters and would appreciate any tips you could throw my way.

    I would welcome any suggestions of achieving cross version compatibility other than referencing all of the office assemblies.


    Ken Slovak MVP - Outlook
    Friday, September 23, 2011 8:49 PM