Outlook not always executes an script from a rule

G

Guest

Hello all,

I have the following code, that is used in a rule when a message arrives.
The rule should take a message with "newticket" on the subject and move it to
helpdesk_unprocessed folder and run the script.

The problem is that it runs sometimes and sometimes not. When it does not
run, if I open the Visual Basic Editor and delete objItem As MailItem from
the beggining, leaving the parethensis (), i can run the macro and always it
works.

I'm really confused, any help? thanks very much,
Carlos

Sub executeMacro(objItem As MailItem)
Dim outApp As New Outlook.Application
Dim nsp As Outlook.NameSpace
Dim mpf As Outlook.MAPIFolder
Dim mpfSubFolder As Outlook.MAPIFolder
Dim flds As Outlook.Folders
Dim msg As Object
Dim msgSubject As String
Dim summary As String
Dim description As String
Dim processed_folder As Outlook.MAPIFolder

Set nsp = outApp.GetNamespace("MAPI")
Set mpf = nsp.GetDefaultFolder(olFolderInbox)
Set flds = mpf.Folders
Set mpfSubFolder = flds.GetFirst
Set processed_folder = flds("Helpdesk_Processed")
Do While Not mpfSubFolder Is Nothing
If mpfSubFolder.Name = "Helpdesk_Unprocessed" Then

For Each msg In mpfSubFolder.Items
If InStr(1, msg.Subject, "newticket") <> 0 Then

MsgBox ("hola")

msg.Move processed_folder

End If
Next
End If
Set mpfSubFolder = flds.GetNext

Loop
End Sub
 
K

Ken Slovak - [MVP - Outlook]

Not everything that arrives in your inbox is an email. You can get other
types of items there. Use Object instead of MailItem. Once you get the item
in the macro test for its Class or MessageClass to see if it is a mail item
and then you can assign the Object to MailItem if you want.

If you are moving/deleting items in a loop you never should use a For...Each
or up-counting For loop. Use a down-counting loop since the loop counter is
being altered:

For i = Count To 1 Step -1 'etc.
 
S

Sue Mosher [MVP-Outlook]

if I open the Visual Basic Editor and delete objItem As MailItem from
the beggining, leaving the parethensis (), i can run the macro and always it
works.

That's because without the MailItem parameter, it's a straight macro. It would also "always work" if you called it as is from a macro that provides any item as the MailItem parameter.

My point is that your code doesn't actually use the MailItem parameter as it's intended to do. There's nothing inherently with your approach. I just want to make sure you understand that, as written, your code process *all* the items in the first subfolder of the Inbox. This is not the way "run a script" rules are normally designed. The usual purpose of a "run a script" rule is to perform some action ***on the received message***. Your code doesn't act directly on the received message at all!

See http://www.outlookcode.com/d/code/quarexe.htm for an example of a "run a script" rule procedure that moves incoming items to another folder.

Another problems:

Dim outApp As New Outlook.Application

Never use that in Outlook VBA code, where you have an intrinsic Application object to work with. You can do this instead:

Dim outApp As Outlook.Application
Set outApp = Application

or just remove the Dim statement completely and replace outApp with Application throughout.
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

Thanks for your answers Ken and Sue, I'll look at them and come back with
results.

Thansk very much,
Carlos
 

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