none
How to modify Outlook template body using c# RRS feed

  • Question

  • I have outlook template with some content.

    I have to replace the content of template and save into drafts using C#.net

    I am using below solution.

    Outlook.Application application = new Outlook.Application();

    Outlook.MailItem mail = application.CreateItemFromTemplate(@"Some path") as Outlook.MailItem;

    mail.Body = mail.Body.replace("-","XXX"); (*** getting error in this line)

    mail.Save();

    While I am trying with above code. I got the below Error.

    Err Msg: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
    Err Code: -2147467260

    I used Microsoft.Office.Interop.Outlook;

    Please provide me teh solution.

    Thanks in Advance!

    Tuesday, September 6, 2016 7:47 AM

All replies

  • Hi Sravanthi,

    Since your question is more related to development, I'm moving it to the dedicated Outlook for Developer forum, there you should get more professional responses:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=outlookdev

    Regards,

    Ethan Hua


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, September 7, 2016 10:07 AM
  • Hello Sravanthi,

    The Body property of the MailItem class returns a String representing the clear-text body of the Outlook item. So, you can get the current string representation, do whatever you need with it and then set it back.

    string body = mail.Body;
    string changedBody = body.replace("-","XXX"); 
    mail.Body = changedBody;
    

    Also I'd recommend calling the Save method before doing anything. Take a look at the How To: Create a new Outlook message based on a template article for the sample code.

    Finally, you may find the Chapter 17: Working with Item Bodies article helpful.


    [custom.development]

    Wednesday, September 7, 2016 1:11 PM
  • >>>While I am trying with above code. I got the below Error.

    Err Msg: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
    Err Code: -2147467260<<<

    According to your description, I have create one Console Application project to try to reproduce this issue, unfortunately, I am not able. Could you help us figure out which type of project you got this exception?

    Thanks for your understanding.
    Thursday, September 8, 2016 4:58 AM
  • HI

    Thanks for the Response..!!!

    I have tried with below code, when ever i try to access 'mail.body' i am getting the exception.

    If i try to read the data from inbox emails , in this case also i am getting the same exceptions.

    string body = mail.Body;
    string changedBody = body.replace("-","XXX");
    mail
    .Body = changedBody;

    I have tried calling Save method before modifying the email, in this case also i am facing same exception if i access mail.body.

    Thanks in advance...

    Thursday, September 8, 2016 5:02 AM
  • Hi David,

    Thanks for the response...!!

    I am using c# console application project

    Thursday, September 8, 2016 7:11 AM
  • Where and when do you run the code? Is it called from a service?

    [custom.development]

    Thursday, September 8, 2016 5:02 PM
  • Hi Sravanthi Ch,

    Do you always get this exception when Outlook is opened or closed? 
    Friday, September 9, 2016 1:33 AM
  • Hi David,

    Thanks for the Response....!!

    I am not getting this exception when ever i open ad close outlook.

    Below are the exception scenarios .

    • Whenever i try to Access any folder(inbox,drafts..etc) email body in c# console application , i am getting the exception.
    • I have created one mail item from template,when ever i try to modify the body of the email ,i am getting the exception.
    • If I check Mailitem properties CC,BCC and few other properties are also throwing same exception

    I am able to access Mail.subject of all the emails without any issue.

    Most of the properties are throwing exception Other than Mail.Subject .

    Thanks in Advance...

    Friday, September 9, 2016 4:57 AM
  • Hi Eugene,

    Thanks for the response...!!

    I am not calling .exe from Service.

    I am manually running exe on my machine.

    which has outlook2013 and VS 2010 and .net framework version 4.0.

    Thanks in advance...

    Friday, September 9, 2016 5:52 AM
  • Hi Sravanthi Ch,

    According to your description, I suggest that you could run your project on an other machine? Do you get same issue?
    Monday, September 12, 2016 1:42 AM
  • Hello Sravanthi,

    The Code you wrote is perfect and it will work just as you desire if you remove the tiny error you created. I really don't know why people are giving such high level solutions for this issue.

    Well the error you made is the 'r' in the mail.Body = mail.Body.replace("-","XXX"); will be a capital 'R' and not a small 'r', as defined in the used library. Just make that change and your code will execute just fine. :) 

    Tuesday, April 10, 2018 11:16 PM