PDA

View Full Version : email closed textfile from Word 2002



cathgill
08-26-2005, 01:12 AM
Hi. I want Word to email a text file. I tried RoutingSlip - this only works if the file is open - I could live with that, but when the file arrives at the recipient's inbox, it's renamed to filename.doc rather than filename.txt.

This is the code I used:

ChangeFileOpenDirectory "C:\OmniScribe\Admin\"
Documents.Open FileName:="WorkLog.txt", ConfirmConversions:=False
DocumentToEmail = "WorkLog.txt"
Documents(DocumentToEmail).HasRoutingSlip = True
With Documents(DocumentToEmail).RoutingSlip
.Subject = DocumentToEmail
.AddRecipient Recipient:="frednurk@bigpond.net.au"
.Delivery = wdAllAtOnce
End With
Documents(DocumentToEmail).Route

You can see that the code doesn't save the text file once it's opened it, so I don't see why Word would convert it during the emailing process.

Ideally, does anyone know code to email the text file without opening it? Alternatively, is there any way to stop Word from renaming the file in the process?

I have Outlook Express - the only thing I can guarantee about the users of the code is that they'll have Word 2000 + and some sort of email.

I looked at a thread called "Mail From Word Using standard MailClient" and was getting quite excited until I saw "ActiveDocument" at the end of the code.

Thank you, thank you in advance - Cath

Killian
08-26-2005, 02:46 AM
Hi and welcome to VBAX :hi:

You were right to be excited about Daniel's excellent post (http://www.vbaexpress.com/forum/showpost.php?p=7016&postcount=1) - the "SendIt" routine is exactly what you want, you just need to call it in a different way.
From the Function declaration you can see that it requires 4 paramaters to be passed to it Function SendIt(sRecip$, sTitle$, sText$, sFile$) As Booleanthe recipient, the mail title, the mail body text and the path to the attached file.
In his example, ActiveDocument.FullName is used for the attached file path - if you don't want to open the file, you just need to find another way of getting the full path of the text file you want to attach. (This can be done a number of ways - inputbox, file dialog, etc). If you don't mind opening the file, then "ActiveDocument.FullName" will still be correct (i.e with a .txt extension), assign that path to a variable, close the text file without saving and call SendIt using your variable.

cathgill
08-26-2005, 11:14 PM
Hi, Killian. I amended the code as follows:

Sub SendWorklogDoc()
FileToEmail$ = "C:\OmniScribe\Admin\Worklog.txt"
SendIt "johnsmith@bigpond.net.au", "This is a test", "Some text in the mail", FileToEmail$
End Sub

and received this error from Microsoft Outlook:

"Either there is no default mail client or the current mail client cannot fulfil the messaging request. Please run Microsoft Outlook and set it as the default mail client."

then an error message titled "Error Sending", "Error: -2147467259".

I use Outlook Express - can the code be modified to run with OE? Is there any way that code can detect the user's default mail client?

Thanks again - Cath

Killian
09-05-2005, 08:47 AM
Hi Cath,

sorry I didn't pick up on this thread earlier...
I tested the code at work using Outlook 2003 and it seems to work fine. I'll see if I can set up a PC with OE so I can test it (might be a while).
This code sets up a mail using the MAPI standard and assumes that there is a mail client that can handle it - if the mail client on the user's PC doesn't handle MAPI, then it can't work. This leads us to your question about finding out a PC's mail client (then you would have to write code for each type you want to support). I'm not sure how to do this - may take a little research.

Going back to you original post... I think any VBA method for sending mail will require the user's default mail client to be MS Exchange in some form, or it will fail.
The reason the text file is changed to a doc when you set the RoutingSlip method is that a RoutingSlip and it's properties are part of the a Word document's structure (a text file is just text)
Another option would be to use the sendmail method (again would require Exchange) but because there's no info passed to the mail object, the user would have to enter the recipientPublic Const MY_FILE_PATH As String = "C:\...some file path...\filename.txt"

Sub SendTheTextFile()

Dim myFile As Document

Set myFile = Documents.Open(FileName:=MY_FILE_PATH, ConfirmConversions:=False)
Options.SendMailAttach = True
myFile.SendMail
myFile.Close False

End Sub