A Better way to Run Scripts in Rule Wizard



Hello again -

You may note that I posted before "Funky Errors running scripts from
Outlook". The strange thing there was for no apparent reason the rule would
fail BEFORE THE CODE EVEN RAN (oh my!)

Anyway, a collegue discovered this posting in MS Knowledgebase which seems
to avoid the problem. The link is:
http://support.microsoft.com/kb/292063/en-us

Here's the code:

In ThisOutlook Session Module:

Public WithEvents myOlItems As Outlook.Items


Public Sub Application_Startup()

' Reference the items in the Inbox. Because myOlItems is declared
' "WithEvents" the ItemAdd event will fire below.
Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items

End Sub


Private Sub myOlItems_ItemAdd(ByVal Item As Object)
' <or whatever you want to do>
' If it's currently not between 9:00 A.M. and 5:00 P.M.
If Time() < #9:00:00 AM# Or Time() > #5:00:00 PM# Then

' Check to make sure it is an Outlook mail message, otherwise
' subsequent code will probably fail depending on what type
' of item it is.
If TypeName(Item) = "MailItem" Then

' Forward the item just received
Set myForward = Item.Forward

' Address the message
myForward.Recipients.Add "myaddress@xxxxxxxxxxxx"

' Send it
myForward.Send

End If

End If

End Sub

This code works really well and does not have the problemthat I encountered
trying to directly run a script from Rules Wizard.

Thought you all should know.

Chris (ct60)





.



Relevant Pages

  • Re: COM Addin Problem after Update to Outlook 2003
    ... It worked with Outlook 2000 and XP, ... Only the Addin wasn't loaded. ... > Private Sub IDTExtensibility2_OnAddInsUpdateAs Variant) ... > On Error GoTo ErrHandler ...
    (microsoft.public.outlook)
  • Re: COM Addin Problem after Update to Outlook 2003
    ... It worked with Outlook 2000 and XP, ... Only the Addin wasn't loaded. ... > Private Sub IDTExtensibility2_OnAddInsUpdateAs Variant) ... > On Error GoTo ErrHandler ...
    (microsoft.public.outlook.program_addins)
  • Re: VBA shutdown error Outlook
    ... In Outlook VBA there is an intrinsic Application object. ... Dim oExpl As Outlook Explorer ... > Public myFlag As Boolean ... > Exit Sub ...
    (microsoft.public.office.developer.outlook.vba)
  • Re: Unable to add "X-Header" to a mail that is being sent...
    ... OutlookSpy - Outlook, CDO ... MsgBox "Initialize Event Handlers successful" ... Private Sub Command1_Click ... ChangeHeader() from my ItemSend event handler, ...
    (microsoft.public.outlook.program_vba)
  • Re: Request for help with VBScript...
    ... exists under Outlook 2000 SP3 and later by rewriting the application to ... Sub s_init ... On Error Goto 0 ... Set lo_inbox = go_namespace.GetDefaultFolder(olFolderInbox) ...
    (microsoft.public.outlook)