PDA

View Full Version : Copy text from texfile into the body of email Outlook 2013



mcubeles
01-26-2016, 01:49 PM
Hi
I am new in the forum.
Need some help with this piece of code
-I have a file on the Desktop (text file)
-want to open read the file and copy all the content
-paste the content into body of the email
the piece of code I have is the following, it works opening the file, read the content and put into a variable.
But when trying to put the content into the body of email nothing happen. :(



Sub copyText()
Dim objOutlook As Outlook.Application
Dim itmMail As Outlook.MailItem

Set objOutlook = Outlook.Application
Set itmMail = objOutlook.CreateItem(0)
'I try to change the objOutlook.CreateItem(oldMailItem) nothing

Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
myFile = "C:\Users\mcubelesgil\Desktop\mytxt.txt"

Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & vbCrLf & textline
Loop
Close #1
With itmMail

.Body = text
.BodyFormat = olFormatHTML
End With

End Sub

when I put the variable text into msgbox it shows the content of the file, no problem with that.
using Outlook 2013 Any help much appreciate

gmayor
01-27-2016, 03:05 AM
What is the content of the text file? Is this html code?
Can we assume you are running this from Outlook VBA?

mcubeles
01-27-2016, 12:48 PM
What is the content of the text file? Is this html code?
Can we assume you are running this from Outlook VBA?

Hi thanks for the reply.
the file is plain text, no html no formated at all.
and yes i am running this from Outlook VBA.

Cheers
M

gmayor
01-28-2016, 12:27 AM
In that case
Option Explicit

Sub copyText()
Dim olInsp As Outlook.Inspector
Dim itmMail As Outlook.MailItem
Dim wdDoc As Object
Dim oRng As Object
Dim myFile As String, strText As String, textline As String

Set itmMail = CreateItem(0)
'I try to change the objOutlook.CreateItem(oldMailItem) nothing
'That's because it should be (olMailItem)
myFile = "C:\Users\mcubelesgil\Desktop\mytxt.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
strText = strText & vbCrLf & textline
Loop
Close #1
With itmMail
.BodyFormat = olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
oRng.text = strText
.Display 'This line is required!
End With
lbl_Exit:
Set olInsp = Nothing
Set itmMail = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
Exit Sub
End Sub

mcubeles
01-28-2016, 01:46 PM
In that case
Option Explicit

Sub copyText()
Dim olInsp As Outlook.Inspector
Dim itmMail As Outlook.MailItem
Dim wdDoc As Object
Dim oRng As Object
Dim myFile As String, strText As String, textline As String

Set itmMail = CreateItem(0)
'I try to change the objOutlook.CreateItem(oldMailItem) nothing
'That's because it should be (olMailItem)
myFile = "C:\Users\mcubelesgil\Desktop\mytxt.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
strText = strText & vbCrLf & textline
Loop
Close #1
With itmMail
.BodyFormat = olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
oRng.text = strText
.Display 'This line is required!
End With
lbl_Exit:
Set olInsp = Nothing
Set itmMail = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
Exit Sub
End Sub

Thanks very much
Works fantastic
Cheers
M