Very easy question for VB expert

  • Thread starter Thread starter Only_me
  • Start date Start date
O

Only_me

Hi, I just started with VB to create a VERY simple Outlook 2000
script.

It just takes the first email in a folder, gives it the title "a", and
then tests the title. How come the third check fails (see **** ERROR
IS HERE).

I am obviously stupid. Please help.


Sub test()
Set CurFolder = Application.ActiveExplorer.CurrentFolder
Set AllItems = CurFolder.Items
Set thisMail = AllItems.Item(1)

thisMail.Subject = "a"

If Not thisMail.Subject = "a" Then
MsgBox "ERROR: Pre-save"
End If

thisMail.Save

If Not thisMail.Subject = "a" Then
MsgBox "ERROR: Post-save"
End If

Set thisMail = AllItems.Item(1)

If Not thisMail.Subject = "a" Then
MsgBox "ERROR after get item again" '**** ERROR IS HERE
End If

thisMail.Subject = "b"
thisMail.Save

End Sub



Note: I tried removing all the "sort by" columns, in case the mails
were being sorted by subject or something. I still get the problem. I
used the "step into" to see what was happening line-by-line.

If I just keep pressing F8 to step-through, I get the same error.
HOWEVER, the weird thing is that if I hover the mouse over one of the
"thisMail"'s in the code as I step through, IT WORKS!!

This is repeatable every time. My brain hurts.
 
You cannot rely on the index of an item in the Items collection. While the
item is open originally get its EntryID, save it and use that to get the
item again using Application.GetNameSpace("MAPI").GetItemFromID(strEntryID).
 
What do you mean by "error"? Do you mean the MsgBox is coming up or do
you really get an error? If the latter one, which error do you get?
 
Back
Top