Runtime error -1629224689

N

noone

Hello all,
The code below works fine with one message selected,
however, it crashes when multiple ones are selected with
the above runtime error.

The crash occurs between the ========= lines
But I think it is because of this line:
Set myForward = oExp.Selection.Item(1).Forward

Any ideas ?

Dim oExp As Outlook.Explorer
Set oExp = Outlook.ActiveExplorer
Set objSelection = oExp.Selection

If objSelection.Count > 0 Then
For Each objItem In objSelection
If TypeOf objItem Is MailItem Then
Set SafeItem = CreateObject("Redemption.SafeMailItem")
Set myForward = oExp.Selection.Item(1).Forward
SafeItem.Item = myForward
=======================================
With SafeItem
.Recipients.Add "#Unsolicited Email"
.Body = "Forwarded via Outlook Anti-Spam"
.DeleteAfterSubmit = True
.Send
End With
=======================================
objItem.Delete
End If
Next
End If
 
N

noone

i think i fixed it, changed:
Set myForward = oExp.Selection.Item(1).Forward
to
Set myForward = objItem.Forward
 
S

Sue Mosher

Eek! You're deleting inside a For Each ... Next loop.
Don't do it! Instead use a loop that counts down from
Selection.Count to one and forwards and deletes the
current item in the loop, not the oExp.Selection.Item(1).
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
N

noone

can you help ? i am completely new to this, this is my first attempt
and i was happy that it was even working ;)

i was getting all confused with this count and that count, loops, etc.
 
N

noone

i had figured out how to do the forwarding, but now i'm not clear if my
for/next loop
is "correct". is there a better way to process the selections ? i guess
there is since i read
an "eek" in your reply ;)
 
S

Sue Mosher [MVP]

A countdown loop would look like this:

intCount = objSelection.Count
For i = intCount to 1 Step -1
Set objItem = objSelection(i)
' do stuff with objItem
Next

--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top