1

I want to write some VBA code that will automatically create a new email from an RTF document. I'm using the following programs: 1. Microsoft Word 2013 2. Microsoft Outlook 2013

I have managed to do everything I want except how to paste the content that I copied into the body of the email.

I have searched all over the web for how to do this however I have not found any simple way of doing this. In addition, all of the examples that I have found were related to Microsoft Excel. I have noticed that there is a difference when using Microsoft Word.

Below is the code that I have written:

Sub SendDocAsMail()

Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim TheUser As String
Dim Subject As String
Dim ClientRef As String
Dim Body As String
Dim Signature As String
Dim SigString As String
Dim i As Integer
Dim Pos As Integer
Dim myAttachments As Outlook.Attachments

TheUser = Environ("UserName")

On Error Resume Next

'Start Outlook if it isn't running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
    Set oOutlookApp = CreateObject("Outlook.Application")
End If

'Create a new message
Set oItem = oOutlookApp.CreateItem(olMailItem)

'Copy the open document to subject and body

'Change only Mysig.htm to the name of your signature
    SigString = Environ("appdata") & _
                "\Microsoft\Signatures\" & TheUser & ".htm"

Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Subject = Selection.Text
Subject = Left(Subject, Len(Subject) - 1)
ClientRef = Subject
ClientRef = Right(ClientRef, Len(ClientRef) - 1)
For i = 1 To Len(ClientRef)
    If Mid(ClientRef, i, 1) = "|" Then
        Pos = i
    End If
Next i
ClientRef = Left(ClientRef, Pos - 1)

Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeParagraph
Selection.InsertFile (SigString)

Selection.WholeStory
Selection.Copy

oItem.To = "xxxx@xxxx.co.il; xxxx@xxxx.co.il"
oItem.BCC = "xxxx@xxxx.co.uk"
oItem.Subject = Subject
'oItem.Body = 'NEED HELP
'Selection.PasteAndFormat (wdFormatOriginalFormatting)

oItem.Display

Set myAttachments = oItem.Attachments
'myAttachments.Add.PathName = "C:\Users\" & TheUser & "\Dropbox\PATENT\Bressler\" & ClientRef & "\"
'Clean up
'    Word.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'    Word.Application.Quit SaveChanges:=wdDoNotSaveChanges


End Sub

All help in pasting the copy text with the original formatting would be greatly appreciated.

David Zemens
  • 53,033
  • 11
  • 81
  • 130
Eitan Waks
  • 57
  • 6
  • 15

1 Answers1

0

Get a handle on the MailItem's Inspector which has a .WordEditor (basically MS Word Document instance)

https://msdn.microsoft.com/en-us/library/office/ff868098.aspx

This should do the trick:

oItem.To = "xxxx@bxxxx.co.il; xxxx@xxxx.co.il"
oItem.BCC = "xxxx@docs.xxxx.co.uk"
oItem.Subject = Subject
'oItem.Body = 'NEED HELP

Dim mailWord as Object 'WordEditor
oItem.Display
Set mailWord = oItem.GetInspector.WordEditor
mailWord.Range(0).PasteAndFormat (wdFormatOriginalFormatting)

Explanation

The explanation is fairly simple. In order to use a method like PasteSpecial you need to be working with an object that has that method available. The MailItem class does not directly have this, but it does contain the Inspector.WordEditor which is a word Document -- so any method you would use in word should be available to the olItem.Inspector.WordEditor.

FOLLOW-UP:

I would just use the FileDialog to select files to attach, like so:

Dim filePicker As FileDialog
Dim fileName As Variant

Set filePicker = Application.FileDialog(msoFileDialogFilePicker)
filePicker.AllowMultiSelect = True

'### specify the folder default for the fileDialog object
filePicker.InitialFileName = "C:\Path\to\your\folder\" 
filePicker.Show

For Each fileName In filePicker.SelectedItems
    oItem.Attachments.Add (fileName)
Next

Alternatively, and this may be simpler or it may be more problematic as is sometimes the case when you hand the thread over to another application:

olItem.GetInspector.CommandBars.ExecuteMSO "AttachFile"

I would prefer the FileDialog method just because it gives you more control over the resulting selections.

David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • Thank you so much for giving me such a quick response. I have been tackling this issue for three days now. Can you possibly give me a quick explanation of why this works so that I can learn the reasoning behind the code. Also, it seems to me that you would be the right person to ask the following question: I would like the macro to open up the attachments dialogue to a specific folder. I couldn't find any method that fits the bill. What would be my best way of implementing the above? – Eitan Waks Sep 29 '15 at 18:11
  • See revisions to my answer above :) – David Zemens Sep 29 '15 at 18:25
  • Thank you so much for all of your help!!! Out of curiosity, why would the file dialog work as opposed to using something from the Attachments object? The first place I looked to find a method was in the Attachments object. In my going about coding the wrong way? – Eitan Waks Sep 29 '15 at 19:31
  • The `FileDialog` method uses the `olItem.Attachments` object and adds each -- this is explicitly *part of the VBA code*. If you rely on the `.ExecuteMSO` method, that should also work. They should both work. My preference would just be for explicit solution, rather than handing the process over to something that is outside of the program's execution code. – David Zemens Sep 29 '15 at 19:34