none
c# Outlook and Application.CreateItemFromTemplate() relative path not working (help) RRS feed

  • Question

  • I have this function

    static void Main(string[] args)
            {
               
                Outlook.Application application = new Outlook.Application();
                
                Outlook.MailItem mail = application.CreateItemFromTemplate(
                    "emailTemplate.oft") as Outlook.MailItem;
                mail.Subject = "My Subject";
                mail.To = "zara@zoro.bu; bora@zora.zu";
                mail.SaveAs("asdf.oft");
            }

    But it is not working. Program crash when try open emailTemplate.oft. This file is under debug folder and when I provide full path to the file like this

    "D:\full\path\to\the\file\emailTemplate.oft"

    then file is opened and my code is working. Same for saveAs() function. It need full path. If I use FileStream for instance I don't need use full path for open file which is under my application exe file. How can I use CreateItmeFromTemplate() function to open file similar to FileStream? Is it possible?


    Thursday, August 2, 2018 3:03 PM

Answers

  • Hi,

    It seems that your path is set incorrectly. Could you please try this:

     Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
     Microsoft.Office.Interop.Outlook.MailItem mail = application.CreateItemFromTemplate(
     AppDomain.CurrentDomain.BaseDirectory+ "\\test.oft") as Microsoft.Office.Interop.Outlook.MailItem;
     mail.Subject = "My Subject";
     mail.To = "test.xx.com";
     mail.SaveAs("asdf.oft");

    Hope it works for you.

    Thanks,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    • Marked as answer by Striebrovlas Friday, August 3, 2018 2:27 PM
    Friday, August 3, 2018 10:18 AM
    Moderator

All replies

  • Hi,

    It seems that your path is set incorrectly. Could you please try this:

     Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
     Microsoft.Office.Interop.Outlook.MailItem mail = application.CreateItemFromTemplate(
     AppDomain.CurrentDomain.BaseDirectory+ "\\test.oft") as Microsoft.Office.Interop.Outlook.MailItem;
     mail.Subject = "My Subject";
     mail.To = "test.xx.com";
     mail.SaveAs("asdf.oft");

    Hope it works for you.

    Thanks,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    • Marked as answer by Striebrovlas Friday, August 3, 2018 2:27 PM
    Friday, August 3, 2018 10:18 AM
    Moderator
  • Thanks.

    Now it is working

    Outlook.Application application = new Outlook.Application();
                Outlook.MailItem mail = application.CreateItemFromTemplate(
                    AppDomain.CurrentDomain.BaseDirectory + "\\emailTemplate.oft"
                     ) as Outlook.MailItem;
                 mail.Subject = "HA .. IT IS WORKING !";
                 mail.To = "zara@zoro.bu; bora@zora.zu";
                mail.SaveAs(
                   AppDomain.CurrentDomain.BaseDirectory + "\\asdf.oft");

    Friday, August 3, 2018 2:28 PM
  • The lead was burried here.  Look at the awesome email addresses...
    Tuesday, May 21, 2019 7:51 PM