共用方式為


使用郵件範本建立郵件專案

此範例會使用 CreateItemFromTemplate 方法建立郵件專案。

範例

此程式代碼範例會開啟 Ivy.oft 範本檔案、指派主旨,然後將訊息儲存至 Drafts 資料夾。

如果您的 Outlook 窗體範本檔案 (.oft) 儲存在要作為訊息範本的磁碟上, CreateItemFromTemplate 方法就很有用。 範本檔案可以包含您想要包含在訊息中的預先格式化文字、信箋或影像。 不過,如果範本檔案包含表單後方的程式代碼,則不會執行表單程式代碼。

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.

Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub CreateItemFromTemplate()
    Dim folder As Outlook.Folder = _
        CType(Application.Session.GetDefaultFolder( _
        Outlook.OlDefaultFolders.olFolderDrafts), Outlook.Folder)
    Dim mail As Outlook.MailItem = _
        CType(Application.CreateItemFromTemplate( _
        "c:\ivy.oft", folder), Outlook.MailItem)
    mail.Subject = "Congratulations"
    mail.Save()
End Sub
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();
}

另請參閱