PDA

View Full Version : copy from Word Paste to Outlook



vbtoto
06-10-2008, 12:32 PM
Hi,

I am opening a Word file, finding and replacing words, copying the contents of it (Select All) and pasting it in New Message in Outlook, however, I want the format to be pasted as well.

Here is the code that I have:

Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Set wordApp = CreateObject("Word.Application")
With wordApp
.Visible = False
Set wordDoc = .Documents.Open("filename")
End With

Dim wordSelection As Selection
Set wordSelection = wordApp.Selection

With wordSelection.Find
.Text = "SOMETHING"
.Forward = True
.MatchWholeWord = True
.Replacement.Text = Trim(txtSomething.Value)
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With

' I go on replacing more texts

wordDoc.Select

Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem

Set objOutlook = CreateObject("Outlook.Application")

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
.Subject = "Subject"
.BodyFormat = olFormatRichText
.Body = wordSelection.Text
.Display
End With

Set objOutlook = Nothing

'closes the word document, does not save changes and does not prompt to save changes
wordDoc.Close (False)
wordApp.Quit ' close the Word application
Set wordDoc = Nothing
Set wordApp = Nothing

End of Code.

As you can see I am using .Body = wordSelection.Text to paste in Outlook, I tried .Body = wordSelection.PasteFormat after using wordSelection.CopyFormat, it gave me an error.

Any help will be appreciated.

fumei
06-10-2008, 01:18 PM
As it seems you are creating both an instance of Word, and an instance of Outlook, what application are you in fact running this from?

Try wordSelection, not wordSelection.Text

I am wondering about that you do not seem to being using a NameSpace.

vbtoto
06-10-2008, 01:23 PM
Hi Fumei,

Tried your suggestion and getting the same result; Outlook does get populated with the text from Word but is not formatted (as per the format in the Word file)

Yes, I am not using Namespaces, what exactly do you mean, I have referenced the objects by references libraries...not sure if this is what you were asking.

Thanks.

vbtoto
06-10-2008, 01:24 PM
Fumei,

I am running this from an Access Form. Thanks again