Results 1 to 8 of 8
  1. #1
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    For Each ... Attachments.Delete (OL2kSR1)

    Following directly on JScher2000's code from <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Cat=&Board=out&Number=74719>this</A> post, I have an issue I don't understand from the following partial code (msgbox testing redundancies introduced by me):

    Sub VeryOdd()
    Dim itmMail As MailItem, attAttMsg As Attachment
    If TypeName(ActiveInspector.CurrentItem) <> "MailItem" Or _
    ActiveInspector.CurrentItem.Attachments.Count = 0 Then Exit Sub
    Set itmMail = ActiveInspector.CurrentItem
    For Each attAttMsg In itmMail.Attachments
    MsgBox attAttMsg.FileName
    Next attAttMsg
    For Each attAttMsg In itmMail.Attachments
    MsgBox attAttMsg.FileName
    attAttMsg.Delete
    Next attAttMsg
    End Sub

    Only the first, third, fifth, seventh, etc., attachments get deleted. How do I fix?
    -John ... I float in liquid gardens
    UTC -7ąDS

  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: For Each ... Attachments.Delete (OL2kSR1)

    it's deleting every other attachment because of the way you are moving to the next one. when you remove one attachment, the next one becomes #1.

    the preferred method is to work backwards so the count doesn't get messed up.

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: For Each ... Attachments.Delete (OL2kSR1)

    Thanks Mary. The following works fine

    For intC = itmMail.Attachments.Count To 0 Step -1
    itmMail.Attachments(intC).Delete
    Next intC

    I still don't know enough about Collections to know that this would be necessary in a For Each loop. Ignorance never seems to lead me to bliss.
    -John ... I float in liquid gardens
    UTC -7ąDS

  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: For Each ... Attachments.Delete (OL2kSR1)

    <img src=/S/blush.gif border=0 alt=blush width=15 height=15> A lapse in testing. Sorry about that.

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: For Each ... Attachments.Delete (OL2kSR1)

    Hi Jefferson. No problem. I plan to write a version of your code to fit my preferences, but I'm learning the OL object from scratch, so I'll be pestering this Forum with questions and occasionally posting code where I get lost. My only other experience is with Excel code, at which I'm not especially strong, and the Excel object is pretty clearly defined. To exacerbate my problems, my IS team is restricting OL VB files for security reasons, so I'm not sure if I have all the Help files I need.

    My preferred version of your code would allow me to save the file anywhere I want, but then still go through the steps of stamping the filename(s) into the e-mail and deleting the attached file(s).

    Right now I'm wondering if OL has something like an Application.Dialogs(olDialogSaveAttachment).Show method, or how I can otherwise get to that Save Attachments dialog to display. (I thought about using the BeforeAttachmentSave event, but I can't delete 'em first and then save 'em!)

    Finally, it should have been

    For intC = itmMail.Attachments.Count To <font color=red>1</font color=red> Step -1

    somehow deleting that zero'th attachment wasn't working well!
    -John ... I float in liquid gardens
    UTC -7ąDS

  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: For Each ... Attachments.Delete (OL2kSR1)

    The object model leaves much to be desired. You can pop up the File, Save Attachments... dialog (different from the one on the right-click context menu) this way:

    <pre>Sub TestPopDialogByID()
    Dim cbtn As CommandBarButton
    Set cbtn = ActiveInspector.CommandBars.FindControl(, 3167)
    cbtn.Execute
    Set cbtn = Nothing
    End Sub</pre>

    I got that control ID this way:
    <pre>Sub cBarInfo()
    Dim ctlBtn As CommandBarControl
    For Each ctlBtn In ActiveInspector.CommandBars("File").Controls
    Debug.Print ctlBtn.Id & "=" & ctlBtn.Caption
    Next
    End Sub</pre>

    I think these IDs are set at the factory, but can't be 100% sure!

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: For Each ... Attachments.Delete (OL2kSR1)

    Eeeha, thank you!

    I'm a dummy, your code wouldn't initially work; then I discovered (doh!) that it only fires if an Item is open which contains an Attachment. Now if I can trap the Cancel from the Save Attachment dialog, I will have what I want.

    Once again, the powers that sign my paycheck want me to work on other things first.
    -John ... I float in liquid gardens
    UTC -7ąDS

  8. #8
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: For Each ... Attachments.Delete (OL2kSR1)

    <img src=/S/hmmn.gif border=0 alt=hmmn width=15 height=15> ActiveInspector.CommandBars.FindControl(, 3167).Execute doesn't return anything.
    -John ... I float in liquid gardens
    UTC -7ąDS

Posting Permissions

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