More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

forum 30-Jul-2014 04:02

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.

Login Password
Remember me
Register | Send my password
    Page [ 1 ]  
 Basic Outlook Printer Friendly Version
Working with Outlook items, folders, recipients; dealing with security; writing event handlers
Topic
Macro to Insert Object (Create From File)
Hello,

I have a word document that I want to be able to insert into the body of an email, either new or a reply, using a macro.

In Excel I recorded the following macro.

ActiveSheet.OLEObjects.Add(Filename:= _
        "K:\My Documents\Outlook\Advanced Search Text.doc", Link:=False, _
        DisplayAsIcon:=False).Select

I've searched this forum and others but I haven't had any success finding a solution.

Would someone be able to assist me with this request?

Thanks and regards,
Chris

  29-May-2009  13:10
  29-May-2009  14:24   
See if the example at http://www.outlookcode.com/codedetail.aspx?id=1333 fits your scenario.
  29-May-2009  15:33   
Thanks Sue,

This definitely is close to what I require. I modified your code to the following.

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:="K:\My Documents\Outlook\Advanced Search Text.doc", ReadOnly:=True)
    Set itm = doc.MailEnvelope.Item
    With itm
        .To = ""
        .Subject = "Test"
        .Save
        ID = .EntryID
    End With
    doc.Activate
    wd.Visible = True
    doc.Windows(1).EnvelopeVisible = True
    
    Set itm = Nothing
    doc.Close wdDoNotSaveChanges
    Set doc = Nothing
    
    Set itm = Application.Session.GetItemFromID(ID)
    'itm.Send
    'doc.Close wdDoNotSaveChanges
    If blnWeOpenedWord Then
    wd.Quit
    End If
    
    Set doc = Nothing
    Set itm = Nothing
    Set wd = Nothing
End Sub

It creates a new message which sometimes is required but the text that I'm inserting is typically inserted into a reply to a received message. How would the code need to be modified to accomplish this?

Additionally, the code currently creates the message and saves it to the Drafts folder, which is fine but I would like the message to be displayed so that I can review it before sending.

Also, the code currently doesn't close Word.

Any assistance would be appreciated.

Thanks and regards,
Chris
  29-May-2009  15:37   
If you want to display the message, just call its Display method.

Word isn't being closed probably because the doc.Close statement is commented out.

Both the new message that this code creates and any reply created separately would have both Body and HTMLBody properties that contain the content of the item in plain text and HTML format respectively.
  29-May-2009  15:38   
Also your Outlook version and, if Outlook 2003 or earlier, the editor type may be relevant.
  29-May-2009  15:55   
Thanks Sue,

I am running Outlook 2003 and I currently don't use Word to edit email messages. If I check this in the Mail Format Options tab Word does close properly. Is there a way to have Word 2003 close without enabling this?

I was able to get the new message to display by changing the code itm.Send to itm.Display.

How would I get the text from the word document to be inserted into a reply? What I would like to do is either open a reply manually and then run the macro to insert the text. Alternately, I could select the message and run the macro which would open the reply and then insert the text.

Thanks,
Chris
  29-May-2009  16:01   
Did you remove the comment on the statement I indicated?

Again, the content in both messages is contained in the Body and HTMLBody properties. Use one or the other to manipulate the content in the message you want to send.
  29-May-2009  16:08   
I did remove the comment on the following line.

doc.Close wdDoNotSaveChanges

Unfortunately, Word still doesn't close.

Regarding your comment about the messages. I unfortunately am not understanding what you mean.

What I have tried to do is reply to a message and then run the macro from that window. When I run the macro, rather than inserting the text into the reply a new message is created.

I hope that this helps to clarify what I'm after. Please let me know if I need provide additional information.

Thank you for your assistance.

Chris
  30-May-2009  11:39   
Hello,

I was finally able to resolve the issue of Word not closing by restarting Outlook.

However, I still don't know what I need to do to get the text, from my word document, inserted into the body of a reply.

Any pointers would be appreciated. Thanks.

Regards,
Chris
  30-May-2009  13:13   
[SOLVED]

I am keeping Sue's code for new messages and I found the following code, that I modified, to use for replies.

Public Sub ReplyWithHTML()
Dim oMail As Outlook.MailItem
Dim oFSO
Dim oFS

    If Application.ActiveExplorer.Selection.Count Then
        If TypeOf Application.ActiveExplorer.Selection(1) Is Outlook.MailItem Then
        Set oMail = Application.ActiveExplorer.Selection(1).Reply
        
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        
        Set oFS = oFSO.OpenTextFile("K:\My Documents\Outlook\Advanced Search Text.htm")

        stext = oFS.readall
        oMail.BodyFormat = olFormatHTML
        oMail.HTMLBody = stext & vbCr & oMail.HTMLBody
        oMail.Display
        End If
    End If
End Sub

It works great.

Chris
    Page [ 1 ]