Results 1 to 12 of 12
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding attachments using VBA (Outlook 2000 SR1)

    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

  2. #2
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Roanoke area, Virginia, USA
    Posts
    3,729
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding attachments using VBA (Outlook 2000 SR1)

    what code are you using to attach the files?

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding attachments using VBA (Outlook 2000 SR1)

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

    objOutlookMsg..Attachments.Add strFileName

    Does this what you looking for MaryJ?

  4. #4
    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: Adding attachments using VBA (Outlook 2000 SR1)

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

  5. #5
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding attachments using VBA (Outlook 2000 SR1)

    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

  6. #6
    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: Adding attachments using VBA (Outlook 2000 SR1)

    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?

  7. #7
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding attachments using VBA (Outlook 2000 SR1)

    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

  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: Adding attachments using VBA (Outlook 2000 SR1)

    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.

  9. #9
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding attachments using VBA (Outlook 2000 SR1)

    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

  10. #10
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding attachments using VBA (Outlook 2000 SR1)

    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

  11. #11
    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: Adding attachments using VBA (Outlook 2000 SR1)

    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??

  12. #12
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding attachments using VBA (Outlook 2000 SR1)

    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

Posting Permissions

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