none
...Does not contain a definition for "session" RRS feed

  • Question

  •        

    In this case I seem to be conflicting with the Systems.Windows.Forms class. I'm trying to make a call to Outlook. I prepended  Application.Session with "Outlook." (I have "using Outlook = Microsoft.Office.Interop.Outlook;" defined) but that doesn't seem to work. I'm not sure I'm instantiating the namespace correctly. My C# is pretty rusty.   

    private void CreateItemFromTemplate()

    {

        Outlook.Folder folder =
            Application.Session.GetDefaultFolder(
            Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
        Outlook.MailItem mail =
            Application.CreateItemFromTemplate(
            @"c:\ivy.oft", folder) as Outlook.MailItem;
        mail.Subject = "Congratulations";
        mail.Save();

                
    Wednesday, July 2, 2014 5:23 PM

Answers

  •  private void CreateItemFromTemplate()
             {
    
                 Outlook.Folder folder =
                     OutlookApp.Session.GetDefaultFolder(
                     Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
                 Outlook.MailItem mail =
                     OutlookApp.CreateItemFromTemplate(
                     @"c:\ivy.oft", folder) as Outlook.MailItem;
                 mail.Subject = "Congratulations";
                 mail.Save();
    
    
    
             }
         }

    So

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

    • Marked as answer by Breaker1253 Wednesday, July 2, 2014 6:46 PM
    Wednesday, July 2, 2014 6:35 PM

All replies

  • Where and how is Application defined?

    Unless your code is in an Outlook addin, you need to have something like the following:

    Outlook.Application OlApp = new Outlook.Application();
    Outlook.Folder folder = OlApp.Session.GetDefaultFolder(
    Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
    Outlook.MailItem mail =
             olApp.CreateItemFromTemplate(
             @"c:\ivy.oft", folder) as Outlook.MailItem;
    mail.Subject = "Congratulations";
    mail.Save();


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


    Wednesday, July 2, 2014 5:30 PM
  • It is (will be hopefully) an Outlook addin. I should have mentioned that earlier. 
    • Edited by Breaker1253 Wednesday, July 2, 2014 5:41 PM
    Wednesday, July 2, 2014 5:41 PM
  • Try the following code:

    private void CreateItemFromTemplate()
    
     {
    
         Outlook.Folder folder =
             Globals.ThisAddin.Application.Session.GetDefaultFolder(
             Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
         Outlook.MailItem mail =
             Application.CreateItemFromTemplate(
             @"c:\ivy.oft", folder) as Outlook.MailItem;
         mail.Subject = "Congratulations";
         mail.Save();

    Wednesday, July 2, 2014 5:44 PM
  • Here's what I get:

    Globals.ThisAddin.Application.Session.GetDefaultFolder(

    ....the name 'globals does not exist in the current context

    and

    Application.CreateItemFromTemplate(

    "System.windows.forms.application does not contain a definition for createitemfromtemplate"

    Wednesday, July 2, 2014 5:48 PM
  • Do you develop a VSTO based add-in? Or are you trying to automate Outlook from a standalone windows form application?

    If you develop an add-in, you can find the the Application property in your add-in class (inherited from a base and provided by the VSTO base classes).

    Wednesday, July 2, 2014 6:05 PM
  • I was using ADX (Add-in-Express). I basically just created a simple button and was creating an on click event. Here's the entire thing so far.

    using System;
    using System.Runtime.InteropServices;
    using System.ComponentModel;
    using System.Windows.Forms;
    using AddinExpress.MSO;
    using Outlook = Microsoft.Office.Interop.Outlook;

    namespace OutlookTemps
    {
        /// <summary>
        ///   Add-in Express Add-in Module
        /// </summary>
        [GuidAttribute("7BA6E1FF-DECA-4C8A-AEED-18E90E8EAB9D"), ProgId("OutlookTemps.AddinModule")]
        public partial class AddinModule : AddinExpress.MSO.ADXAddinModule
        {
            public AddinModule()
            {
                Application.EnableVisualStyles();
                InitializeComponent();
                // Please add any initialization code to the AddinInitialize event handler
            }

            #region Add-in Express automatic code

            // Required by Add-in Express - do not modify
            // the methods within this region

            public override System.ComponentModel.IContainer GetContainer()
            {
                if (components == null)
                    components = new System.ComponentModel.Container();
                return components;
            }

            [ComRegisterFunctionAttribute]
            public static void AddinRegister(Type t)
            {
                AddinExpress.MSO.ADXAddinModule.ADXRegister(t);
            }

            [ComUnregisterFunctionAttribute]
            public static void AddinUnregister(Type t)
            {
                AddinExpress.MSO.ADXAddinModule.ADXUnregister(t);
            }

            public override void UninstallControls()
            {
                base.UninstallControls();
            }

            #endregion

            public static new AddinModule CurrentInstance
            {
                get
                {
                    return AddinExpress.MSO.ADXAddinModule.CurrentInstance as AddinModule;
                }
            }

            public Outlook._Application OutlookApp
            {
                get
                {
                    return (HostApplication as Outlook._Application);
                }
            }

            private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)
            {
               
            }

            private void CreateItemFromTemplate()
            {

                Outlook.Folder folder =
                    Globals.ThisAddin.Application.Session.GetDefaultFolder(
                    Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
                Outlook.MailItem mail =
                    Application.CreateItemFromTemplate(
                    @"c:\ivy.oft", folder) as Outlook.MailItem;
                mail.Subject = "Congratulations";
                mail.Save();



            }
        }
    }

    Wednesday, July 2, 2014 6:31 PM
  •  private void CreateItemFromTemplate()
             {
    
                 Outlook.Folder folder =
                     OutlookApp.Session.GetDefaultFolder(
                     Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
                 Outlook.MailItem mail =
                     OutlookApp.CreateItemFromTemplate(
                     @"c:\ivy.oft", folder) as Outlook.MailItem;
                 mail.Subject = "Congratulations";
                 mail.Save();
    
    
    
             }
         }

    So

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

    • Marked as answer by Breaker1253 Wednesday, July 2, 2014 6:46 PM
    Wednesday, July 2, 2014 6:35 PM