Results 1 to 8 of 8
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA to insert text of file in e-mail message (Outlook 2000)

    I'm attempting to write a procedure that will insert a specific file at the end of an Outlook e-mail message. I want to insert the text of my ASCII file directly into the message, not as an attachment. The only thing is that I haven't figured out how I can do this!

    It looks like one approach may be to use the Open statement to open the file and then use the Get statement to read the file and assign all of the text to a variable. I wasn't able to make this work.

    Any clues about how to use Open or Get or any other approach will be greatly appreciated.

    - Linda

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: VBA to insert text of file in e-mail message (Outlook 2000)

    What kind of a variable are you using and how are you trying to use it? I would think you could use the Open an Get to read each line of the text file and then write that directly into the body of the message. Are you having trouble with writing the text to the messag body or with getting the text from the file?
    Charlotte

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA to insert text of file in e-mail message (Outlook 2000)

    Charlotte,

    I'm having trouble understanding the information in Help for Open and Get, so I have no clue what arguments to provide.

    I was trying something like:

    Dim objOutlook As Outlook.Application
    Dim itmMail As Outlook.MailItem
    Dim strFile As String
    Dim strText As String

    strFile = "c:file.txt"
    Open strFile For Binary Access Read As #1
    Get #1, , strText 'How do I tell it to read the entire file and assign it to strText?
    Close #1

    strMessage = itmMail.Body & vbCrLf & strText
    itmMail.Body = strMessage


    I want the entire file to be assigned to the variable (strText). The length of the file will vary on each person's computer, but it'll be about 30 lines of text; nothing too lengthy. One of the optional arguments for the Open statement is the length, but how do I know what the length is? It will vary.

    Thanks, Charlotte.

    - Linda

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: VBA to insert text of file in e-mail message (Outlook 2000)

    Length is used to pass record length for fixed width records or the length of the block of text to return. If it's left out, you should get everything up to 32K of data. Of course, if the file is bigger than that, you have a problem. If you want sequential input, try opening the file this way:

    Open strFile For Input As #1

    As long as the file is less than 32k, your get statement should work. Of course, there is also a message length limit in Outlook, so you'll have to duck that issue as well.
    Charlotte

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: VBA to insert text of file in e-mail message (Outlook 2000)

    Linda,

    If you can set a reference to the MicroSoft Scripting Runtime (SCRRUN.DLL), the following code is probably the simplest way to do what you want, especially if the text file is not particularly large.

    You will need to insert the appropriate replacement text/variables where I have used red.<pre>Sub MailTextFile()
    Dim objOutLook As Outlook.Application
    Dim objMail As Outlook.MailItem
    Dim fso As Scripting.FileSystemObject
    Dim tsTextIn As Scripting.TextStream
    Dim strTextIn As String
    Set objOutLook = New Outlook.Application
    Set objMail = objOutLook.CreateItem(olMailItem)
    Set fso = New Scripting.FileSystemObject
    Set tsTextIn = fso.OpenTextFile(<font color=red>"C:File.txt"</font color=red>)
    strTextIn = tsTextIn.ReadAll
    With objMail
    .To = <font color=red>"eMailAddress"</font color=red>
    .Subject = <font color=red>"Subject"</font color=red>
    .Body = <font color=red>"Message "</font color=red> & vbCrLf & strTextIn
    .Send
    End With
    Set objMail = Nothing
    Set objOutLook = Nothing
    Set tsTextIn = Nothing
    Set fso = Nothing
    End Sub</pre>


    Andrew C

  6. #6
    2 Star Lounger
    Join Date
    Jan 2001
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA to insert text of file in e-mail message (Outlook 2000)

    I am delighted to say that I was able to get both methods to work. Yah! Thank you both very much!

    - Linda

  7. #7
    2 Star Lounger
    Join Date
    Jan 2001
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA to insert text of file in e-mail message (Outlook 2000)

    I would like to ask one more related question if I may.

    Just when I thought I was done with this, I realized that my program should not add the inserted file at the end of the message. It needs to go at the end of the *current* message in the case of a reply. So, if there are original messages displayed, those should stay at the bottom.

    I was thinking I could have the user position the insertion point where the file is to be inserted and then run the macro. It sounds good, but how can I insert the file at the insertion point?

    - Linda

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VBA to insert text of file in e-mail message (Outlook 2000)

    SendKeys <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    I have been using a SendKeys based solution to insert a footer at the insertion point. It's not completely reliable, though; sometimes for no apparent reason the insertion point will leap up into the To field or some other random place halfway through and then spew text into the wrong place. Basically, the standard Outlook editor just wasn't designed to be programmed this way...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •