• Adding attachments using VBA (Outlook 2000 SR1)

    Home » Forums » AskWoody support » Productivity software by function » MS Outlook and email programs » Adding attachments using VBA (Outlook 2000 SR1)

    Author
    Topic
    #367520

    Hi there

    I have some VBA code which creates and email and files in the relevant detals (To email address, Subject, and Body) and also attaches a document to the email.

    The problem comes in that I have a list of up to 7 documents which I want to attach to the email. But when I have a look at the email message once the VBA code has finish, I notice that only one of the documents has been attached and its the last one that is in the list.

    Does anyone know how to attach all 7 documents using VBA or got any code examples?

    James

    Viewing 0 reply threads
    Author
    Replies
    • #573221

      what code are you using to attach the files?

      • #573226

        Set objOutlook = GetObject(, “Outlook.Application”)
        Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

        objOutlookMsg..Attachments.Add strFileName

        Does this what you looking for MaryJ?

        • #573258

          I think she would want to see the 7 calls to the .Add method to rule out coding problems…

          • #573279

            On thinking about my reply, I did neglect to provide all of the information, thanks for pointing that out.

            Sorry about that.

            I have an array containing the filenames and once I have given the Outlook Item the relevant details (email address, subject and body); I then loop through an array which contains the filenames and I use the one line of code:
            .Attachments.Add strFileName
            to add the attachment(s) to the email.

            The catch comes in that if there is more than 1 file to be attached, only the last one is actually attached to the email instead of all of the files.

            Is this explanation a little clearer? (Sorry again for my sloppiness earlier!)

            James

            • #573285

              If you put a

              MsgBox “Loop counter is ” & intCounter & vbCrLf & “File name is ” & strArray(intCounter)

              inside the loop, does it show that it does loop the requisite number of times and find the right file names?

              Actually, you are saying strFileName, but are you using an array index there?

            • #573292

              I am using an array index to store the filenames.

              The first line in the loop retrieves the filename from the array and stores it in the variable strFilename (Reason for this boils down to readibility).

              I have put in the line of debug code you suggested and it definitely does touch each filename in the array index.

              Here is a good question though. Would the Outlook MailItem.Attachments.Add have an index of it’s own which I should be refering to if I am attaching more than 1 file to the email message?

              James

            • #573293

              The short answer to your last question is that there may well be an index, but when adding an item to the collection, you ignore it.

              I looked at some code I use, and it’s essentially the same as yours –

              myMessage.Attachments.Add strFilePath, olByValue

              One thing you might try is saving the message after you add each attachment. If they are large, this will slow things down, but it’s worth a shot.

            • #573615

              Hi there

              Each file is between 5k and 45k so size is not a problem.

              I have tried that and it still doesn’t work. Was definitely worth the shot though, thanks for the idea. I guess I’ll have to figure out how to manipulate the collection a bit more. Anyone know offhand how to do that? Or know where I can find out?

              James

            • #576849

              Hi there!

              Found the darn problem! Its the mode of viewing things again! Just because you can’t see it, doesn’t mean its not there!

              Just after the olByValue, one can specify the position (viewing position of the attachment I guess?), so if youhave more than 1 attachment, try incrementing this number……

              James

            • #576989

              Interesting. Is your message in a format other than plain text? That might explain the difference. Should have asked that before.

              The online help leaves a big question: are the parameters of Position in relation to other attachments or…what??

            • #577024

              I am using Microsoft Outlook Rich Text.

              I am guessing that there are set positions to display the attched file’s icon and one just has to specify which one you want to use. There might be some other relationship of some kind, but your guess would be as good as mine on this issue.

              Thanks for the help anyway! You definitely helped to point me in the right direction there.

              James

    Viewing 0 reply threads
    Reply To: Adding attachments using VBA (Outlook 2000 SR1)

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

    Your information: