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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » VBA to insert text of file in e-mail message (Outlook 2000)

    Author
    Topic
    #360893

    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

    Viewing 0 reply threads
    Author
    Replies
    • #544477

      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?

      • #544502

        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

        • #544542

          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.

        • #544614

          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.

          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("C:File.txt")
          strTextIn = tsTextIn.ReadAll
              With objMail
                  .To = "eMailAddress"
                  .Subject = "Subject"
                  .Body = "Message " & vbCrLf & strTextIn
                  .Send
              End With
          Set objMail = Nothing
          Set objOutLook = Nothing
          Set tsTextIn = Nothing
          Set fso = Nothing
          End Sub

          Andrew C

          • #544708

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

            – Linda

            • #544717

              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

            • #545290

              SendKeys smile

              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…

    Viewing 0 reply threads
    Reply To: VBA to insert text of file in e-mail message (Outlook 2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: