none
Create a VBA / Macro to open an email template RRS feed

  • Question

  • I was hoping that someone would be able to help me out. I would like to create a macro / VBA (depending on which is best) to open an user email template. Is this possible and if so, how would I? I don't know where to even start. I can get into the editor though.

    Monday, July 11, 2016 3:03 PM

Answers

  • Hello,

    VBA is used for macros in Office applications. You can use the CreateItemFromTemplate method of the Application class which creates a new Microsoft Outlook item from an Outlook template (.oft) and returns the new item.

    Sub CreateFromTemplate() 
     Dim MyItem As Outlook.MailItem 
     
     Set MyItem = Application.CreateItemFromTemplate("C:\statusrep.oft") 
     MyItem.Display 
    End Sub 
     
    Sub CreateTemplate() 
     Dim MyItem As Outlook.MailItem 
     
     Set MyItem = Application.CreateItem(olMailItem) 
     MyItem.Subject = "Status Report" 
     MyItem.To = "Dan Wilson" 
     MyItem.Display 
     MyItem.SaveAs "C:\statusrep.oft", OlSaveAsType.olTemplate 
    End Sub

    Also you may find the How To: Create a new Outlook message based on a template article helpful.


    [custom.development]

    Monday, July 11, 2016 4:36 PM

All replies

  • Hello,

    VBA is used for macros in Office applications. You can use the CreateItemFromTemplate method of the Application class which creates a new Microsoft Outlook item from an Outlook template (.oft) and returns the new item.

    Sub CreateFromTemplate() 
     Dim MyItem As Outlook.MailItem 
     
     Set MyItem = Application.CreateItemFromTemplate("C:\statusrep.oft") 
     MyItem.Display 
    End Sub 
     
    Sub CreateTemplate() 
     Dim MyItem As Outlook.MailItem 
     
     Set MyItem = Application.CreateItem(olMailItem) 
     MyItem.Subject = "Status Report" 
     MyItem.To = "Dan Wilson" 
     MyItem.Display 
     MyItem.SaveAs "C:\statusrep.oft", OlSaveAsType.olTemplate 
    End Sub

    Also you may find the How To: Create a new Outlook message based on a template article helpful.


    [custom.development]

    Monday, July 11, 2016 4:36 PM
  • Hi NoviceVBAuser1775

    According to your description, you could follow Eugene's suggestion and sample code, in addition more information about VBA in Outlook, you could refer about Getting Started with VBA in Outlook 2010.


    Tuesday, July 12, 2016 9:47 AM
  • thank you for this information.
    Tuesday, July 19, 2016 8:08 PM
  • Could this be used from inside an Access database?  This would be great to add to an Access database that I am using.  Also, is there a way to add book marks to a outlook template like in the salutation where I can say take this contact's name place it at this bookmark?
    Tuesday, July 19, 2016 8:12 PM
  • >>>Could this be used from inside an Access database? 

    According to your description, yes, because Microsoft Outlook supports Automation, you can control Outlook from any program that is written with Microsoft Visual Basic. Automation provides a 

    standard method for one application to access the objects, methods, properties, and events of other applications that support Automation.

    To start an Outlook Automation session, you can use either early or late binding. Late binding uses either the Visual Basic GetObject function or the CreateObject function to initialize Outlook. 

    To use early binding, you first need to set a reference to the Outlook object library. Use the Reference command on the Visual Basic for Applications (VBA) Tools menu to set a reference to 

    Microsoft Outlook xx.x Object Library, where xx.x represents the version of Outlook that you are working with. 

    For more information, click here to refer about Automating Outlook from a Visual Basic Application

    >>>Also, is there a way to add book marks to a outlook template like in the salutation where I can say take this contact's name place it at this bookmark?

    As far as I know that there is not a way to add book marks to a outlook template. For plain text you can read the MailItem.Body property into a string variable and use the standard VBA string functions to parse and edit the text. For HTML items you can read HTMLBody into a string and work with that. But that will be raw HTML.

    Thanks for your understanding.
    Thursday, August 18, 2016 7:45 AM