PDA

View Full Version : Copy (with Format) from Word, Paste in Outlook



vbtoto
06-13-2008, 05:47 AM
Hi
From Access, 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.

I posted this msg earlier but it seems its died down, so I have to post it again. Here is the code


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



In objOutlookMsg.Body using both wordSelection.Text or just wordSelection pastes the contents but not the format.

I tried using objOutlookMsg.Body = wordSelection.PasteFormat, after using wordSelection.copy above but the PasteFormat gives an error.

I would really appreciate some help.

Nelviticus
06-16-2008, 05:06 AM
Instead of wordSelection.Text try wordSelection.Range.FormattedText.

Regards

vbtoto
06-16-2008, 07:24 AM
Hi Nelvicus,

wordSelection.Range.FormattedText does paste the contents in Outlook BUT does NOT apply the format that is in the Word File. Thanks for trying.

CreganTur
06-16-2008, 07:59 AM
Instead of forcing the document into Outlook, have you considered e-mailing the document via Word itself?

I know that:Options.SendMailAttach = False
ActiveDocument.SendMail
Will open a message window for sending the specified document through Microsoft Exchange with the document text as the e-mai body text (instead of as an attachment).

Could this solve your problem?

Nelviticus
06-16-2008, 08:55 AM
Hmm, well I've done a bit of playing around without success - the problem is really an Outlook one, i.e. "how do I programatically insert formatted content into an Outlook message". I found a thread and FAQ on Tek-Tips where someone achieves this by creating the Outlook mail directly from the Word doc using 'wordDoc.MailEnvelope.Item', although I couldn't get this to work in Office 2007.

You can read the FAQ here: http://www.tek-tips.com/faqs.cfm?fid=6950

Regards

MOS MASTER
06-16-2008, 01:38 PM
Hi vbToto,

What you want is possible but it depends which version of Outlook you're working with.

In Office xp SP3 the Outlook object guard was introduced and a lot of code is threaded as dangerous eversince. (outlook will throw a msg that a program is trying to...)

If your version is before the above version you can try to get a handle to the ActiveInspector of outlook and check it it is the WordEditor. After this get a handle to that inspector and threat it like a normal Word document.

Something like:

Sub FillOutlookBody()
ActiveDocument.Content.Copy
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objInspector As Outlook.Inspector
Dim objDoc As Word.Document
Set objOutlook = CreateObject("Outlook.Application")

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.Subject = "Subject"
.BodyFormat = olFormatHTML
.Display
End With
Set objInspector = objOutlook.ActiveInspector
If Not objInspector Is Nothing And objInspector.EditorType = olEditorWord Then
Set objDoc = objInspector.WordEditor
objDoc.Range.Paste
End If
Set objDoc = Nothing
Set objOutlookMsg = Nothing
Set objInspector = Nothing
Set objOutlook = Nothing
End Sub


So perhaps this will work for you.

To recap the tip CreganTur gave you (SendMailAttach) is a well know trick to do this. Offcourse it's not as full featured as using the full Outlook object model but if it's enough for you I'd go for it.

HTH