Unable to access mail settings for Outlook 2010 Add-In application C#

Answered Unable to access mail settings for Outlook 2010 Add-In application C#

  • Monday, April 16, 2012 5:43 AM
     
     

    Hi,

     I have developed outlook 2010 add-in application for reply with attachment using c#.

    User has been added prefix for Replies and Forwards in mail settings. File>>Option>>Mail>>Replies and Forward

     Preface comments with "[SCT]".

     When replying to an email message in Outlook 2010 add-in applicaiton I have set to prefix for each line of the original message with"[SCT]".

    But it does not do that. In Customized Outlook add-in application, the prefix [SCT] is not displaying. Prefix is displaying for normal Outlook reply and forward option.

    Below is the code snippet of the add-in application:
      
       
      
     //This Method is used to reply the receiver with attachment(s) using ribbon menu in outlook 2010.
      
       
      
     public void ReplywithAttachment(Office.IRibbonControl control)
      
            {
      
                ReplyWithAttachments(false);
      
            }
      
       
      
     ///This method is used to identify the Outlook email body format.
     /// If email body format is "olFormatHTML" or "olFormatPlain", It will call Outlook Reply/Reply All method.
     /// If email body format is "olFormatRichText", It will call Outlook Forward method.
      
       
      
    public void ReplyWithAttachments(bool isreplyall)
      
    {
      
      Outlook.MailItem rpl = null;
      
      object item;
      
      item = GetCurrentItem();
      
       if (item != null)
      
       {
      
          Outlook.MailItem mailItem = item as Outlook.MailItem;
      
          if (!isreplyall)
      
          {
      
             if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML || mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatPlain)
      
             {
      
               rpl = (Outlook.MailItem)mailItem.Reply();
      
               CopyAttachments(mailItem, rpl);
      
             }
      
             else if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatRichText)
      
            {
      
              rpl = (Outlook.MailItem)mailItem.Forward();
      
            }
      
       
      
         }
      
       rpl.Display();
      
       rpl = null;
      
       item = null;
      
      }
      
     }

All Replies

  • Monday, April 16, 2012 10:53 AM
     
     
    i can also reproduce this, no idea why it behaves like that. As a workaround you could try invoking standard ribbon button by using CommandBars.ExecuteMso("Reply") and then do your further operations. This way, commenting will work
  • Monday, April 16, 2012 2:41 PM
    Moderator
     
     
    In general it's best to wait until the Inspector.Activate() event fires on any item that's being opened before modifying it at all. Otherwise things you try either won't show up or will overwrite things such as signatures. That might also be the case with what's being done here.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "DamianD" <=?utf-8?B?RGFtaWFuRA==?=> wrote in message news:bf24f06a-caf0-4337-be7c-3c01c0053b1a...
    i can also reproduce this, no idea why it behaves like that. As a workaround you could try invoking standard ribbon button by using CommandBars.ExecuteMso("Reply") and then do your further operations. This way, commenting will work

    Ken Slovak MVP - Outlook
  • Wednesday, April 18, 2012 7:26 AM
     
     

    thanks for your reply.

    still i couldn't able to solve this issue.

    Please help me.

  • Wednesday, April 18, 2012 10:48 AM
     
     
    did you try workaround with ExecuteMso("Reply")? it works for me.
  • Thursday, April 19, 2012 7:29 AM
     
      Has Code

    Hi Damian,

    thanks for your reply.

    I have tried, but it's not working for me.I'm getting some error.

    I'm using below code and ribbon xml:

     public void ReplywithAttachment(Office.IRibbonControl control)
      
            {
      
                ReplyWithAttachments(false);
      
            }
      
       
      
     ///This method is used to identify the Outlook email body format.
     /// If email body format is "olFormatHTML" or "olFormatPlain", It will call Outlook Reply/Reply All method.
     /// If email body format is "olFormatRichText", It will call Outlook Forward method.
      
       
      
    public void ReplyWithAttachments(bool isreplyall)
      
    {
      
      Outlook.MailItem rpl = null;
      
      object item;
      
      item = GetCurrentItem();
      
       if (item != null)
      
       {
      
          Outlook.MailItem mailItem = item as Outlook.MailItem;
      
          if (!isreplyall)
      
          {
      
             if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML || mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatPlain)
      

             {

                Outlook.inspector inspector =new outlook.inspector();

               inspector.CommandBars.ExecuteMso("Reply")

      
               rpl = (Outlook.MailItem)mailItem.Reply();
      
               CopyAttachments(mailItem, rpl);
      
             }
      
             else if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatRichText)
      
            {
      
              rpl = (Outlook.MailItem)mailItem.Forward();
      
            }
      
       
      
         }
      
       rpl.Display();
      
       rpl = null;
      
       item = null;
      
      }
      
     }


    <?xml version="1.0" encoding="UTF-8"?><customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">  <ribbon>    <tabs>      <tab idMso="TabReadMessage">        <group id="grpRespond" label="Respond with Attachment"  insertBeforeMso="GroupShow"  >    

    <button id="textButton" label="Reply with Attachment" getImage="GetImage" size="large" screentip="Reply with Attachment" onAction="ReplywithAttachment" supertip="Reply to the sender with attachment of this message." getEnabled="IsVisible"/>   

    <button id="tableButton" label="Reply All With Attachment" getImage="GetImage" size="large" screentip="Reply All With Attachment" onAction="ReplyAllWithAttachment" supertip="Reply to the sender and all other recipients with attachment of this message." getEnabled="IsVisible"/>  

    </group> 

    </tab>  

    <tab idMso="TabMail"> 

    <group id="grpHomeRespond" label="Respond with Attachment"  insertBeforeMso="GroupQuickSteps" > 

    <button id="HomeText" label="Reply with Attachment" getImage="GetImage" size="large" getEnabled="MyButtonEnableCallback" screentip="Reply with Attachment" onAction="ReplywithAttachment" supertip="Reply to the sender with attachment of this message."/>  

    <button id="HomeTable" label="Reply All With Attachment" getImage="GetImage" size="large" getEnabled="MyButtonEnableCallback" screentip="Reply All With Attachment" onAction="ReplyAllWithAttachment" <span style%3

  • Thursday, April 19, 2012 9:17 AM
     
     

    Hi Damian,

    please send me your working code.

    Thanks.

  • Thursday, April 19, 2012 11:57 AM
     
     

    check this link this may help you out

    http://forums.asp.net/t/1792548.aspx/1?Unable+to+add+prefix+settings+in+outlook+Add+In+application

  • Thursday, April 19, 2012 12:03 PM
     
     

    Hi Kevin,

    i have checked that link.

    but no solution was there.

    Thanks.

  • Thursday, April 19, 2012 1:28 PM
    Moderator
     
     
    This line makes no sense:
     
        Outlook.inspector inspector =new outlook.inspector();
     
    You cannot just create an Inspector. You need to either display an existing or new item, or hook into an existing Inspector from ActiveInspector() or from the Inspectors collection.
     
    If you have an item "mailitem" to display an Inspector for it you would use this code:
     
        Outlook.inspector inspector =mailitem.GetInspector();
        inspector.CommandBars.ExecuteMso("Reply");
     
    That will open a new reply item to "mailitem".
     
    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Manikandan_1985" <=?utf-8?B?TWFuaWthbmRhbl8xOTg1?=> wrote in message news:c57f549e-9c31-402c-a16e-fa3a12dcdcc3...

    Hi Damian,

    thanks for your reply.

    I have tried, but it's not working for me.I'm getting some error.

    I'm using below code and ribbon xml:

     public void ReplywithAttachment(Office.IRibbonControl control)
      
            {
      
                ReplyWithAttachments(false);
      
            }
      
       
      
     ///This method is used to identify the Outlook email body format.
     /// If email body format is "olFormatHTML" or "olFormatPlain", It will call Outlook Reply/Reply All method.
     /// If email body format is "olFormatRichText", It will call Outlook Forward method.
      
       
      
    public void ReplyWithAttachments(bool isreplyall)
      
    {
      
      Outlook.MailItem rpl = null;
      
      object item;
      
      item = GetCurrentItem();
      
       if (item != null)
      
       {
      
          Outlook.MailItem mailItem = item as Outlook.MailItem;
      
          if (!isreplyall)
      
          {
      
             if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML || mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatPlain)
      

             {

                Outlook.inspector inspector =new outlook.inspector();

               inspector.CommandBars.ExecuteMso("Reply")

      
               rpl = (Outlook.MailItem)mailItem.Reply();
      
               CopyAttachments(mailItem, rpl);
      
             }
      
             else if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatRichText)
      
            {
      
              rpl = (Outlook.MailItem)mailItem.Forward();
      
            }
      
       
      
         }
      
       rpl.Display();
      
       rpl = null;
      
       item = null;
      
      }
      
     }


    <?xml version="1.0" encoding="UTF-8"?><customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">  <ribbon>    <tabs>      <tab idMso="TabReadMessage">        <group id="grpRespond" label="Respond with Attachment"  insertBeforeMso="GroupShow"  >    

    <button id="textButton" label="Reply with Attachment" getImage="GetImage" size="large" screentip="Reply with Attachment" onAction="ReplywithAttachment" supertip="Reply to the sender with attachment of this message." getEnabled="IsVisible"/>   

    <button id="tableButton" label="Reply All With Attachment" getImage="GetImage" size="large" screentip="Reply All With Attachment" onAction="ReplyAllWithAttachment" supertip="Reply to the sender and all other recipients with attachment of this message." getEnabled="IsVisible"/>  

    </group> 

    </tab>  

    <tab idMso="TabMail"> 

    <group id="grpHomeRespond" label="Respond with Attachment"  insertBeforeMso="GroupQuickSteps" > 

    <button id="HomeText" label="Reply with Attachment" getImage="GetImage" size="large" getEnabled="MyButtonEnableCallback" screentip="Reply with Attachment" onAction="ReplywithAttachment" supertip="Reply to the sender with attachment of this message."/>  

    <button id="HomeTable" label="Reply All With Attachment" getImage="GetImage" size="large" getEnabled="MyButtonEnableCallback" screentip="Reply All With Attachment" onAction="ReplyAllWithAttachment" <span style%3


    Ken Slovak MVP - Outlook
  • Thursday, April 19, 2012 7:19 PM
     
     
    remove your code for invoking Reply method on mailitem. Attach yourself to NewInspector event on Inspectors before calling ExecuteMso and when new inspecotr event fires, access its CurrentItem and invoke your copy of attachments functionality
  • Thursday, April 19, 2012 7:26 PM
    Moderator
     
     
    In NewInspector() you get what's called a "weak" object reference to Inspector and to Inspector.CurrentItem.
     
    That means that little other than Inspector.CurrentItem.MessageClass and Inspector.CurrentItem.Class will be available to you. Accesses to other properties will return nulls or exceptions in most cases.
     
    When the first Inspector.Activate() event fires the item has been fleshed out, and is a strong object reference that can be fully utilized.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "DamianD" <=?utf-8?B?RGFtaWFuRA==?=> wrote in message news:f84d1b89-4be6-4b58-912f-062a573ff9f6...
    remove your code for invoking Reply method on mailitem. Attach yourself to NewInspector event on Inspectors before calling ExecuteMso and when new inspecotr event fires, access its CurrentItem and invoke your copy of attachments functionality

    Ken Slovak MVP - Outlook
  • Friday, April 20, 2012 1:31 PM
     
     

    Hi Damian,

    I have tried in code as per your steps,but still i couldn't able to solve this issue.

    if possible please send me your working code.

    Thanks in Advance.


    Manikandan

  • Friday, April 20, 2012 7:50 PM
     
     
    ok, i will prepare sample code for you, but most probably i will be able to do it on Monday, please be patient.
  • Monday, April 23, 2012 5:08 AM
     
     
    ok sure.i will wait for your reply.

    Manikandan

  • Monday, April 23, 2012 8:47 AM
     
     Answered Has Code

    Change

    public void ReplywithAttachment(Office.IRibbonControl control) 
    { 
      ReplyWithAttachments(false); 
    } 

    on

    public void ReplywithAttachment(Office.IRibbonControl control) 
    { 
      ReplyWithAttachments(false, (Inspector)control.Context); 
    } 
    

    Now change function ReplyWithAttachments with

    public void ReplyWithAttachments(bool isreplyall, Inspector inspector) 

    Now inside

     if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML || mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatPlain) 

    replace everything with

    inspector.CommandBars.ExecuteMso("Reply");
    rpl = (MailItem)inspector.Application.ActiveInspector().CurrentItem;
    CopyAttachments(mailItem, rpl); 

    Now it should work.

  • Wednesday, May 02, 2012 2:16 PM
     
     

    Hi Damian,

    I tried with your solution , its working fine for mail without attachment,but once i added the attachment to the reply item through the code its not working ,

    please advise .

  • Wednesday, May 02, 2012 4:06 PM
     
     
    what do you mean by 'not working'? Original issue with comment value or something else? describe problem in details and show your current code.
  • Thursday, May 03, 2012 5:26 AM
     
     

    Hi Damian,

    I have problem with  comment value ,without the attachment once I edit the comment values are comming ,After that i add the "getAttachments" method to added the attachment file Comment values are not comming,

    I used the below code

    please advise

    using Inspector = Microsoft.Office.Interop.Outlook.Inspector;

     public void Replyattch(Office.IRibbonControl control)
     {
                try
                {
                    SendWithAttachment(Inspector)control.Context);
                }
                catch (Exception ex)
                {
                    WriteLog(ex.ToString());
                }
     }

    public void SendWithAttachment(Inspector inspector)
    {
      try
       {
       
         Outlook.MailItem remail = null;
         object item;
         Microsoft.Office.Interop.Outlook.Application objApp = new Microsoft.Office.Interop.Outlook.Application();
         item = objApp.ActiveInspector().CurrentItem;
         Outlook.MailItem Currentmail= item as Outlook.MailItem;
         inspector.CommandBars.ExecuteMso("Reply");
         remail = (Outlook.MailItem)inspector.Application.ActiveInspector().CurrentItem;
         getAttachments(Currentmail, remail);
       
      }
     catch (Exception ex)
     {
       WriteLog(ex.ToString());
     }
    }


      public void getAttachments(Outlook.MailItem objSourceItem, Outlook.MailItem objTargetItem)
           
            {
                try
                {
       foreach (Microsoft.Office.Interop.Outlook.Attachment _attachment in objSourceItem.Attachments)
                    {
                      string attpath = "C:\\Temp\\" + _attachment.FileName;
                      _attachment.SaveAsFile(attpath);
                            objTargetItem.Attachments.Add(attpath);
                            File.Delete(attpath);
      }

                }

                catch (Exception ex)
                {
                    WriteLog(ex.ToString());
                }
            }

    Thanks in Advance.

  • Thursday, May 03, 2012 6:39 AM
     
     
    i will be able to test your code no sooner then Monday, so please show some patience or maybe someone else wil be able to help you sooner :)
  • Tuesday, May 08, 2012 1:28 PM
     
     

    Hi Damian,

    Any update on my problem

  • Tuesday, May 08, 2012 2:01 PM
     
     

    i was trying to find a workaround but i couldn't. Yes, after modifying Attachments collection, word editor somehow forgets about inserting comments. It seems like a small bug for me.

    As a side note please do not instantiate Application objects by yourself, always use the one from .Application property on almost any outlook api object.

    Sorry i could not help you. Maybe adding attachments later on (during Send event?) is viable for you?

  • Wednesday, May 09, 2012 5:34 AM
     
     

     Hi Damian,

    Thanks for your Time,I changed Application objects as you said