Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy Text From Word And Paste Into Email 1

Status
Not open for further replies.

GarstonMassive

Programmer
May 5, 2006
71
GB
Hi,

as per the subject I'm trying to copy text from a Word doco and paste into an email.

Can anyone point me in the right direction?

TIA.
 
If you doing this from Access set references to Outlook & Word object libraries. Then in code:
1. Create a reference to the Word document.
2. Get the relevant text and store in a string.
3. Create a reference to a new MailItem object and set the Body property to the text string.

This'll give you an unformatted text e-mail.

 
Thanks for your reply Ian.

I figured this was the process but I'm stumped on the second part; would the syntax look something like this:

objWord.Text = strText
 
OK so far I've got:

With objWord
.Visible = True
.Documents.Open(<path>)
.Selection.WholeStory
.Selection.Copy
strBody = .Selection.Paste
End With

Can someone please give me the last push!!!???
 
OK this worked:

Dim varWordContents

With objWord
.Visible = True
.Documents.Open (<path>)
.Selection.WholeStory
.Selection.Copy
varWordContents = .Selection.Range.Text
End With

Ever get the feeling you're talking to yourself....?!
 
No worries Ian.

I've happened another issue; can you keep the formatting of the Word doc and paste it into the body of an email?
 
I've not tried this, but have created HTML e-mails using the code from the site I mentioned.

I reckon the quick and dirty way would be to programmatically create a temporary HTML file from the word document and get the contents like this:

Dim fsoCurrent As FileSystemObject
Dim tsCurrent As TextStream

Set fsoCurrent = CreateObject("Scripting.FileSystemObject")
Set tsCurrent = fsoCurrent.OpenTextFile(strFile, ForReading)
strHTML = tsCurrent.ReadAll

This can be a bit unpredictable so you'll have to suck it and see!

Then set the e-mail item HTML property to this string.
 
Ian sorry for the delay in replying but this is fantastic!! Works a treat and well deserves a star!

Just one small fly in the ointment if I may:

my Word doc (which I save as an HTM file) contains a picture which although transfers into my email OK, doesn't appear at the recepients end.

Any ideas how I can change this? I've tried saving the original file in a different format eg. .mht but that hasn't worked.

Many thanks again!
 
The images aren't actually included in the file. When you create an HTML document from word manually (as it were) images are created in seperate directories. The image tags (<img>...</img> i.e. where to look for the file) are pointed at files in these directories. These are going to be local or, worse, temporary. This is why they don't appear on other machines. The trick is to identify image file locations (image tag properties) & include them in the e-mail.

Last thing first. As far as I'm aware, there are 2 ways of to include an image in an Outlook e-mail:
1 Reference an image on a web server (you'll be prompted to display external images when the e-mail is received).
2 Embed it in the e-mail (possible security issues so the e-mail might be blocked - now or in the future).

The 1st method would involve:
a. Identifying the <img> src property.
b. Copy the file to the server.
c. Rewrite <img> src to point at the image on the server.

The 2nd method would involve:
a. Identifying the <img> src property.
b. Add that file as an attachment to the e-mail.
c. Rewrite <img> src to point at the attachment.
Steps 2 & 3 are covered here:
And now getting the images... You can use the MSHTML type library (set a reference to C:\Windows\System32\MSHTML.tlb or similar) to open your temporary HTML document and loop through all the image tags like this:

Dim objMSHTML As New MSHTML.HTMLDocument
Dim objHTMLDocument As MSHTML.HTMLDocument
dim objImageTags As IHTMLElementCollection
Dim objImageTag As HTMLImg

Set objHTMLDocument = objMSHTML.createDocumentFromUrl([Temp HTML File Path], vbNullString)

While objHTMLDocument.readyState <> "complete"
DoEvents
Wend

Set objImageTags = objHTMLDocument.getElementsByTagName("IMG")

'Loop thru' image tags
For Each objImageTag In objImageTags
---Carry out actions here using objImageTag.src ---
Next objImageTag


I used the Replace function to overwrite the instances of <img src=x> in the HTML string with <img src=y> because I was creating HTML e-mails from custom HTML files and MSHTML likes to rewrite your code(!).

In your case, I think you could rewrite each tag.src property directly and use objHTMLDocument.toString to output the result.

Let me know how you get on.
 
Rats, I forgot that the Replace function ain't available in some versions of Access so here's one I repared earlier(!):

Public Function Replace(ByVal Expression As String, ByVal Find As String, ByVal ReplaceWith As String, Optional ByVal Start As Long = 1, Optional ByVal Count As Long = -1) As String
On Error GoTo Err_Replace
Dim strOutput As String
Dim lngCount As Long
Dim lngFind As Long
Dim lngPosition As Long
Dim lngStart As Long


strOutput = Expression
lngFind = Len(Find)
lngStart = Start
If Len(strOutput) > 0 And lngFind > 0 And Not Count = 0 Then
Do
lngPosition = InStr(lngStart, strOutput, Find)
If lngPosition = 0 Or lngCount = Count Then
Exit Do
End If
strOutput = Left(strOutput, lngPosition - 1) & ReplaceWith & Right(strOutput, Len(strOutput) - lngPosition - lngFind + 1)
lngCount = lngCount + 1
Loop
End If

Exit_Replace:
Replace = strOutput
Exit Function

Err_Replace:
MsgBox "Replace Error: " & Err.Number & ": " & Err.Description
Resume Exit_Replace
End Function
 
Ian that you link you provided was invaluable. I used the code that attaches the images to the email together with manipulation of the original HTML and that did the trick.

Using the FileSystem object was a master-stroke!

Thank you ever so much for your efforts. Your input was invaluable. People like you reaffirm my faith in human nature!

This site contines to be a brilliant resource.
 
Ian I've come across a slight headache -

on some users machines the images aren't displayed in the email. On the majority it's fine. Could this be down to the vageries of FSO?

I've tried comparing the settings on everyones Outlook and nothing obvious has jumped out at me.

Any ideas mate?
 
...some extra info if it helps.

When the email comes back from the original recipient, the image appears!

Weird huh....
 
This is the reference to the image that I use:

"<img src='cid:SomePicture.jpg' height=480 width=500>"


which is attached. Do you require more info?

As I say, it only fails on a couple of machines - the rest are OK and when the mail comes back, the image appears all of a sudden! Strange....
 
As you probably know, different all web browser engines parse HTML strings (to fit standard and custom object models) in their own way - each browser has different error handling. And different browsers also display all information differently - look at any website in IE, Firefox, Netscape, Opera etc. and your very likely to see differences in display and/or functionality.

Incidently, the same argument will apply to different versions of the same browser engine. And because browser engines (or their COM equivalents) are used by web content viewer controls in other programs, different versions of host programs could display web files in different ways.

So, do the problem machines open the e-mails with other mail programs i.e. not Outlook or not a microsoft product?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top