A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Outlook not always executes an script from a rule



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old September 1st 06, 04:59 PM posted to microsoft.public.outlook.program_vba
Carlos
external usenet poster
 
Posts: 24
Default Outlook not always executes an script from a rule

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




Ads
  #2  
Old September 1st 06, 06:26 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Outlook not always executes an script from a rule

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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Carlos" wrote in message
...
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





  #3  
Old September 1st 06, 06:59 PM posted to microsoft.public.outlook.program_vba
Sue Mosher [MVP-Outlook]
external usenet poster
 
Posts: 11,651
Default Outlook not always executes an script from a rule

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
http://www.turtleflock.com/olconfig/index.htm
and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx

"Carlos" wrote in message ...
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




  #4  
Old September 2nd 06, 10:27 AM posted to microsoft.public.outlook.program_vba
Carlos
external usenet poster
 
Posts: 24
Default Outlook not always executes an script from a rule

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

Thansk very much,
Carlos

"Sue Mosher [MVP-Outlook]" wrote:

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
http://www.turtleflock.com/olconfig/index.htm
and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx

"Carlos" wrote in message ...
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





 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Rule 'run a script' not running my script [email protected] Outlook and VBA 3 May 30th 06 12:09 PM
"Run a script" rule triggers but script does not execute Trey Shaffer Outlook and VBA 7 April 7th 06 11:34 PM
Problem running a VBA script from an Outlook rule Olivier Langlois Outlook and VBA 5 March 16th 06 09:03 PM
Problem running a VBA script from an Outlook rule Olivier Langlois Add-ins for Outlook 5 March 16th 06 09:03 PM
Script in Rule clarkel Outlook and VBA 2 February 2nd 06 08:20 PM


All times are GMT +1. The time now is 08:42 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2024 Outlook Banter.
The comments are property of their posters.