More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 23-May-2019 09:47

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Basic Outlook Printer Friendly Version
Title: Send Word document as message
Description: This sample takes advantage of the "Office envelope" feature that allows you to create a message from a document. Tested only in Outlook 2003, but it shoudl work in Outlook 2002 and 2007 as well.
Date: 23-Aug-2006  09:09
Code level: intermediate
Code area: Basic Outlook
Posted by: Sue Mosher
This message is displayed as VB.NET
 Sub SendDocAsMsg()
    Dim wd As Word.Application
    Dim doc As Word.Document
    Dim itm As Object
    Dim ID As String
    Dim blnWeOpenedWord As Boolean
    On Error Resume Next
    Set wd = GetObject(, "Word.Application")
    If wd Is Nothing Then
        Set wd = CreateObject("Word.Application")
        blnWeOpenedWord = True
    End If
    Set doc = wd.Documents.Open _
      (FileName:="C:\Current.doc", ReadOnly:=True)
    Set itm = doc.MailEnvelope.Item
    With itm
        .To = "Address"
        .Subject = "Subject"
        ID = .EntryID
    End With
    Set itm = Nothing
    Set itm = Application.Session.GetItemFromID(ID)
    doc.Close wdDoNotSaveChanges
    If blnWeOpenedWord Then
    End If
    Set doc = Nothing
    Set itm = Nothing
    Set wd = Nothing
End Sub
All 103comments
Page [ 1 2 3 4 5 6 7 8 9 10 Next >>  
  23-Aug-2006  09:12   
Note that this is Outlook VBA code and requiries a reference to the Microsoft Word library in Tools | References.
  24-Jun-2007  14:44   
This was most helpfull thx Sue.
  14-Sep-2007  17:38   
Hi sue,
i found this sample very usefull, but cant feagure how can i
open the item befor sending it ? ( itm.display - did'nt work)
  16-Sep-2007  08:08   

Is it possible to do this from Excel VBA? I'm sending a word documents with mail merge fields as the message body and I would like to make it automatic from Excel, the only problem I've noticed is that .HTMLBody property takes the text from the word file as Unformatted.

  17-Sep-2007  10:16   
Adi, yes, you can modify this code to run from Excel. You'll need to instantiate an Outlook.Application object and use it instead of Application (as in Application.Session.GetItemFromID). Also, if run from Excel, it will be subject to security prompts.

On your other issue, when setting the value of HTMLBody programmatically, you must use fully tagged HTML content. The tags provide the formatting.
  17-Sep-2007  10:16   
AGAMN, to display the item before sending, you must use Word methods:

    wd.Visible = True
    doc.Windows(1).EnvelopeVisible = True

Note that this means only one message can be created and displayed at a time.
  17-Sep-2007  13:39   
Hi sue,
many thanks for your answer !
i'v noticed that the LOGO who's stays at the top heading
of the document wont pass to email page...
is ther any solution for that ?
tanx a lot.
  17-Sep-2007  15:25   
agamn, please provide more details regarding what you mean by "logo who's stays at the top heading." Remember that email messages don't have headers and footers like documents.
  17-Sep-2007  18:13   
Hi sue,
ther is our company logo at the "header and footer" section.
i expected to see it embeded also at the message..?
any suggestion for this need ?
searching your articles i found this code, and i wonder if it possible
to open in this trick a custom form (that will store the logo)?

Set itm = doc.MailEnvelope.Item
strID = itm.EntryID
Set itm = Nothing
Set OL = CreateObject("Outlook.Application")
Set ns = OL.GetNamespace("MAPI")
'tried: Set ns = OL.GetNamespace("MAPI").GetDefaultFolder(6).Items.add(sFormName)...but geting err on getitemFromid.
Set theitem = ns.getitemFromid(strID)
If Not theitem Is Nothing Then
End If

tanx a lot for your time.
Nissan. (is my name)
  18-Sep-2007  01:59   
Sue, Hi again !
Sorry i forgot to indicate that after testing this code
find it more useful to edit the message before the send, instead of editing the word document before the send.
hope you wont get in confusion.
tanx again...
Page [ 1 2 3 4 5 6 7 8 9 10 Next >>