Link to home
Start Free TrialLog in
Avatar of Jill9871
Jill9871

asked on

VBA for Copy and Paste (with formatting) from Word 2010 to Outlook 2010

I have created a Word template that uses a userform - a date field, some list boxes, some text boxes, etc.  Then, when user is done, there are command buttons that call various vba subs - one is "InsertData" which does exactly what I want - which is to take the user input and place it in appropriate positions in a table (yes, I am intentionally using Word rather than Excel - truthfully I am taking a custom routine from Excel and trying to create it in Word, and it was much easier to set up in Excel!).  Then, I want to "EmailData" - where the entire text of the document is placed into the body of a new message window, while holding its table format, with To and Subject prefilled, but display only (user will need to select attachments before sending).  Then, the userform is unloaded, and the Word document will close without saving (or prompting to save).

Everything works great, except that the formatting is not retained.  I've tried various paste methods and range selection methods, and I can't find one that works.  (Note that if I Ctrl+V in the Outlook message window after the code has run (when I have the Range.Copy line included), it pastes exactly as I want it - is there any way to paste clipboard, or even push the keystrokes to the message window??)  Any help would be appreciated.  My "Email Data" code is below.

Private Sub EmailData()

    Dim OutApp As Object
    Dim OutMail As Object
       
    ActiveDocument.Range.Select
    'ActiveDocument.Range.Copy

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        .To = "EmailAddressHere"
        .Subject = "SubjectHere"
        .HTMLBody = ActiveDocument.Range.FormattedText
        .Display
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
    
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Faustulus
Faustulus
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jill9871
Jill9871

ASKER

I had to include ActiveDocument.Range.Select, but otherwise, that worked perfectly - thanks very much!
Jull9871,
Yes, of course. Sorry about that.
Actually, I had intended ActiveDocument.Content.Copy
I'm not a fan of getting VBA to select things for the purpose of manipulating them.
Agreed - I switched to ActiveDocument.Content.Copy.  Thanks again!