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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Jull9871,
Yes, of course. Sorry about that.
Actually, I had intended ActiveDocument.Content.Cop y
I'm not a fan of getting VBA to select things for the purpose of manipulating them.
Yes, of course. Sorry about that.
Actually, I had intended ActiveDocument.Content.Cop
I'm not a fan of getting VBA to select things for the purpose of manipulating them.
ASKER
Agreed - I switched to ActiveDocument.Content.Cop y. Thanks again!
ASKER