Use Word Document in body of email (1 Viewer)

Eljefegeneo

Still trying to learn
Local time
Yesterday, 23:27
Joined
Jan 10, 2011
Messages
904
Using Access 2010 Late Binding

I am attempting to send an email with a word document in the body of the email. I have a form which I can fill out (or is filled out with the data of another form). The code below works fine in previous applications where I had

Code:
.Body = Cstr(Me.MessageBoy)

However, modifying the code to use the word document in the email, I get an error message on the line

Code:
.Body = woApp.ActiveDocument.Content

The error message is:
Runtime Error 2147417851 (800 10 10 5)
Method of 'Body" of object '_MailItem' failed

Here is my code:

Code:
Dim olApp As Object
Dim mItem As Object
Dim woApp As Object
Dim strDocument As String
Dim strTo, strCC2, strBCC2 As String
Dim strSubject, strMessageBody As String

strTo = Me.To
strCC2 = Nz(Me.CC, "")
strBCC2 = Nz(Me.BCC, "")
strSubject = Me.SubjectLine
strDocument = Me.AttachFile

Set olApp = CreateObject("Outlook.Application")
Set mItem = olApp.CreateItem(0)
Set woApp = CreateObject("Word.Application")
woApp.Visible = True
woApp.Documents.Open (strDocument) 'Correct document is open

With mItem
    .To = CStr(strTo)
    .CC = CStr(strCC2)
    .BCC = CStr(strBCC2)
    .Subject = CStr(strDocument)
    .Body = woApp.ActiveDocument.Content  "fails on this line
    .Display    ' To show the email message to the user
   '.Send        'Sends without displaying
End With
End If

' Release all object variables
Set mItem = Nothing
Set olApp = Nothing
Set woApp = Nothing

The correct document is open.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:27
Joined
Oct 29, 2018
Messages
21,455
Hi. Try assigning the content of the Word document into a String variable first. Once you can do that without any errors, then you should be able to use the String variable for your email.
 

Eljefegeneo

Still trying to learn
Local time
Yesterday, 23:27
Joined
Jan 10, 2011
Messages
904
or try to Paste the content to a Hidden textbox (richtext format).
then use the Plaintext content of the textbox as the Body of the Email.

replay #1 in this forum: www.vbforums.com/showthread.php?575977-RESOLVED-Extract-information-from-a-word-doc-using-excel-vba

I tried the paste to the hidden text box and got this when Outlook opened.

Code:
<div><font face="Times New Roman" size=3 color=black>To:you Details </font></div>

<div><font face="Times New Roman" size=3 color=black>please add:</font></div>

<div><font face="Times New Roman" size=3 color=black>1. a button to review all contact records for a certain record and a way to check off &quot;follow up completed&quot; in that form, you had that for pan am..</font></div>

Did not try to figure out the thread from the vbforums. I may when I get a chance but it was way over my head.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:27
Joined
May 7, 2009
Messages
19,230
use PlainText() function to remove all html tags from the richtextbox:

.Body = Plaintext(Me.thRichTextbox)

or use the .HTMLBody property to preserve the formattings.
 

Users who are viewing this thread

Top Bottom