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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy A Word Document File and Not Just Text File 1

Status
Not open for further replies.

SpeedStick

Technical User
May 24, 2007
37
US
I am trying to modify the below code to allow me to copy files other than text files. I have been looking for an alternative to 'TextStream' but I have not been able to find one. Essentially, I would really like to copy Word Documents. Any assistance would be great.

Public Function SendEMail()

Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Set fso = New FileSystemObject
Subjectline$ = InputBox$("Please enter the subject line for this mailing.", _
"We Need A Subject Line!")
If Subjectline$ = "" Then
MsgBox "No subject line, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "E-Mail Merger"
Exit Function
End If
BodyFile$ = InputBox$("Please enter the filename of the body of the message.", _
"We Need A Body!")
If BodyFile$ = "" Then
MsgBox "No body, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "I Ain’t Got No-Body!"
Exit Function
End If
If fso.FileExists(BodyFile$) = False Then
MsgBox "The body file isn’t where you say it is. " & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "I Ain’t Got No-Body!"
Exit Function
End If
Set MyBody = fso_OpenTextFile(BodyFile, ForReading, False, TristateUseDefault)
MyBodyText = MyBody.ReadAll
MyBody.Close
Set MyOutlook = New Outlook.Application
Set db = CurrentDb()
Set MailList = db.OpenRecordset("MyEmailAddresses")
Do Until MailList.EOF
Set MyMail = MyOutlook.CreateItem(olMailItem)
MyMail.To = MailList("email")
MyMail.Subject = Subjectline$
MyMail.Body = MyBodyText
MyMail.Send
MailList.MoveNext
Loop
Set MyMail = Nothing
Set MyOutlook = Nothing
MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing
End Function
 
I've always liked the objects, methods and properties of the Scripting.FileSystemObject, and though there might be faster ways of working (though I'm lead to believe the .ReadAll method is quite fast), I've often used those.

But - in this case, you're just copying the contents of the file, and pasting into the e-mail, couldn't you rather use attachement?

Now, the following is just typed, so there could be errors, but something along the lines of the following?

[tt]Do Until MailList.EOF
Set MyMail = MyOutlook.CreateItem(olMailItem)
With MyMail
.To = MailList("email")
.Subject = Subjectline$
.Body = MyBodyText
.Attachments.Add BodyFile
.Send
End With
MailList.MoveNext
Loop[/tt]

If you really want the contents of the word document, I think you'd need to use Office Automation to retrieve it - i e - programatically open Word, then have Word open the document, read the text and assign to the body text.

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top