Link to home
Start Free TrialLog in
Avatar of BVass
BVass

asked on

Copy Word Doc to Body of Email

Hi,

I am trying to get an open Word document, select all of the text and copy it into an email I am creating using VBA in Access.

The code I am using gets the text and puts it into the email, however I loose the formatting.

I can not find a way to simply cut and paste the text from the Word document to the body of the email.

Please note that I am pretty new at using VBA.

Any help will be greatly appreciated.

Regards,

BVass


'Set Body of Email
wrdApp.Selection.WholeStory
 
'Open Outlook and create new email
Set outApp = New Outlook.Application
Set outMail = outApp.CreateItem(olMailItem)
With outMail
    .To = strEmail
    .Subject = "Brokerage for " & strQtr & " - " & strClient
    .Body = wrdApp.Selection
    .Display
End With

Open in new window

Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

If you manually copy and paste from Word into an email do you keep the formatting?
Avatar of BVass
BVass

ASKER

Hi peter57r,

Yes mate I do.

BVass
Hi BVass,
my work with access/outlook indicates that the outlook object model cannot programmatically accept formatting for text.  Whilst the Outlook front end supports RTF,  the two options with the outlook object model are either HTML or plain text.   If you need to use outlook's inbuilt RTF support you need to use another object model e.g. outlook redemption.  Alternatively there should be a way to get Word to convert its content to html, which presumably could be done before piping it to outlook with the mailitem's .HTMLBody property
Hope this helps!
Nat
Avatar of BVass

ASKER

Nat,

Can you please expand on how to use the HTMLBody property (ie how I should code it) as I am new to VBA for Access.

Thanks

BVass
it's exactly the same as the body property i.e. instead of

.Body = wrdApp.Selection

you use .HTMLBody = "<your HTML here>"

of course the challenge is to get Word to generate the HTML equivalent of its word formatting for you.  Without knowing the in and outs of Word I would suggest that Word's SaveAs Method looks like a worthy candidate as it has a FileFormat argument which includes wdFormatHTML.  So consider after having selected all the text, use VBA to paste it into a new document, save that document as an HTML file, re-open the new file and use the content for the HTMLBody.  Alternatively, I guess you could just open Word in HTML mode (File new web page) and then use the content directly.  My only reservation here is that Word's HTML includes a load of gunk in the header which could be an issue.  Anyway this is stuff you'll have to investigate.

If your requirements allow, an altogether more elegant solution is use the lebans HTML editor which basically gives you a simple editor on an Access form which generates relatively uncluttered HTML directly.  Check out http://www.lebans.com/htmleditor.htm
Avatar of BVass

ASKER

Nat,

I used the following code:
    .HTMLBody=wrdApp.Selection

It puts the text into the body of the email, however all the formatting is lost and it becomes one long sentence.

I looked at the link you put in your last response and it was for a HTML editor for a web browser.  I did not understand it at all.

Is there a way to copy the selected text from the word doc and then paste it in the body of the email.  If I do this manually it works.....I just need the to know the code to be able to paste it into the body of the email.

BVass
Hi BVass,

>> Is there a way to copy the selected text from the word doc and then paste it in the body of the email.

There may be a way to do that but I don't know what it is, and it's also probably more challenging than using HTML.   As a possible alternative, have you considered attaching the Word document rather than including the content directly?  That would be quite straightforward, a bit of a kludge I know.  I reckon it's the easiest way forward unless you're prepared to investigate one of the previously mentionned alternatives more deeply...

Nat
Avatar of BVass

ASKER

Nat,

I have researched your answers already (as I said in my previous post) but I do not understand them at all.  Can you please maybe try to explain how to use the .HTMLBody property in a more detailed fasion?

BVass
ASKER CERTIFIED SOLUTION
Avatar of Natchiket
Natchiket
Flag of United Kingdom of Great Britain and Northern Ireland 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
I found that this works for me. There may be other ways, but I've failed to find them.

'***********************************
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oEmail As MailItem
Dim oOutlook As Object
Dim strBody As String
   
Set oWord = CreateObject("Word.Application")

Set oDoc = oWord.Documents.Open("Document.doc", , True)

'Get the text for the body of the Email
oWord.ActiveDocument.SaveAs FileName:="HTMLDoc.html", FileFormat:=wdFormatHTML
strBody = oDoc.HTMLProject.HTMLProjectItems(1).Text
   
'Open Outlook and create new email
Set oOutlook = New Outlook.Application
Set oEmail = oOutlook.CreateItem(olMailItem)
With oEmail
    .To = "recipient@email.com"
    .Subject = "Document.doc"
    .HTMLBody = strBody
    .send
End With
'****************************************
The solution above very nearly solved my problem.  The text of the Word document came through just fine, and in the correct format.  The one remaining issue is the graphic image (a logo) at the top of the Word document.  It arrives in the email body as an empty box with the dreaded "red X".  I do not have vast knowledge of HTML.  Can anyone assist me with getting the image to display in the email body?