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.