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.
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.