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

incoming message rule to kick off vba with a variable



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old March 21st 08, 05:56 PM posted to microsoft.public.outlook.program_vba
[email protected]
external usenet poster
 
Posts: 4
Default incoming message rule to kick off vba with a variable

I'm new to programming VBA for Oulook but not Access, but here's what
I am trying to do:

I do business with about 25 branches of a particular company.
The branches are identified like this: ABC01, ABC03, ABC23, ABC58,
etc, all 5 digits.

I 'd like to send a message to my "Worker machine" (a box that runs
Access and Outlook) that says in the subject "SendTo ABC03" and it
would start an application (access) with a macro named ABC03 which
would pull the recordset and e-mail the report. I have the back half
of that already done.
What I need is a way to pass the ABC03 from the subject line into the
start application variable to call the proper macro.

I could do this with 25 rules, but I thought there might be an easier
way to do it using VBA.
Ads
  #2  
Old March 21st 08, 06:46 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default incoming message rule to kick off vba with a variable

A rule can call a "script", which is a specially formatted Public Sub in the
Outlook VBA project. For mail items the Sub would look like this:

Public Sub whateverNameYouWant(Item As Outlook.MailItem)
' blah, blah
End Sub

That would let you access the Subject and call to other macros.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


wrote in message
...
I'm new to programming VBA for Oulook but not Access, but here's what
I am trying to do:

I do business with about 25 branches of a particular company.
The branches are identified like this: ABC01, ABC03, ABC23, ABC58,
etc, all 5 digits.

I 'd like to send a message to my "Worker machine" (a box that runs
Access and Outlook) that says in the subject "SendTo ABC03" and it
would start an application (access) with a macro named ABC03 which
would pull the recordset and e-mail the report. I have the back half
of that already done.
What I need is a way to pass the ABC03 from the subject line into the
start application variable to call the proper macro.

I could do this with 25 rules, but I thought there might be an easier
way to do it using VBA.


  #3  
Old March 21st 08, 07:03 PM posted to microsoft.public.outlook.program_vba
JP[_3_]
external usenet poster
 
Posts: 201
Default incoming message rule to kick off vba with a variable

Here's how I automatically run an Excel macro on an attachment to an
email. This requires a reference to the Excel object library from
Outlook. (ToolsReferences in the VB Editor).

This goes at the top of the ThisOutlookSession module:

Private WithEvents SSINBOX As Outlook.Items

Here is the startup event code. If you already have a startup event,
just copy and paste the inner code into it:

Private Sub Application_Startup()
Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")
' monitor specific inbox for incoming msgs
Set SSINBOX = objNS.Folders("Mailbox -
SS_Inbox").Folders("Inbox").Items
End Sub

Here is the event code that monitors the Inbox and passes the macro
name to my function which calls a macro by the same name.

Private Sub SSINBOX_ItemAdd(ByVal Item As Object)

If TypeOf Item Is Outlook.MailItem Then
Dim Msg As Outlook.MailItem
Set Msg = Item
If (Msg.SenderName = "Reporting Application") _
And (Msg.Subject = "Data You Needed") And _
(Msg.Attachments.Count = 1) Then
' run our macro on the attachment
Call ProcessFile("MyMacro", Msg)
Msg.UnRead = False
End If
End If

Set myAttachments = Nothing
Set XLApp = Nothing
Set Msg = Nothing
Set objNS = Nothing
End Sub

The macro name and message are passed to the function. The macro is
stored in my personal workbook:


Function ProcessFile(MacroName As String, Item As Outlook.MailItem)
Dim myAttachments As Outlook.Attachments
Dim XLApp As Excel.Application
Dim XlWK As Excel.Workbook
Dim Att As String
Const attPath As String = "C:\"

Set XLApp = New Excel.Application

' save attachment
Set myAttachments = Item.Attachments
Att = myAttachments.Item(1).DisplayName
myAttachments.Item(1).SaveAsFile attPath & Att

' open personal workbook, just in case
On Error Resume Next
XLApp.Workbooks.Open ("C:\Documents and Settings\jpena\Application Data
\Microsoft\Excel\XLSTART\PERSONAL.XLS")
On Error GoTo 0

' open workbook and run macro
XLApp.Workbooks.Open (attPath & Att)

XLApp.Run ("PERSONAL.XLS!" & MacroName)

XLApp.Workbooks.Close
' delete temp file
Kill attPath & Att
XLApp.Quit

End Function


The event code monitors the Inbox and passes the macro name to my
function which calls a macro by the same name. In your case, you could
substitute this by parsing the subject line, i.e.

Dim MacroToRun As String
MacroToRun = Left$(Msg.Subject, Worksheetfunction.Find("
",Msg.Subject) -1)

So if someone emailed you with the subject "SendTo ABC03", the
variable MacroToRun would contain the "ABC03" string from the Subject
line, which you would then pass to the other function like this:

Call ProcessFile(MacroToRun, Msg)

So ProcessFile would run the ABC03 macro on the email message.

This is all air code so please test first. Keep in mind this would all
happen automatically, without user intervention, so you might see the
screen flicker a bit, this is normal.

I have a sample on my site:
http://codeforexcelandoutlook.com/outlook.html
(check bottom of page)

HTH,
JP




On Mar 21, 12:56*pm, wrote:
I'm new to programming VBA for Oulook but not Access, but here's what
I am trying to do:

I do business with about 25 branches of a particular company.
The branches are identified like this: *ABC01, ABC03, ABC23, ABC58,
etc, all 5 digits.

I 'd like to send a message to my "Worker machine" (a box that runs
Access and Outlook) that says in the subject "SendTo ABC03" and it
would start an application (access) with a macro named ABC03 which
would pull the recordset and e-mail the report. *I have the back half
of that already done.
What I need is a way to pass the ABC03 from the subject line into the
start application variable to call the proper macro.

I could do this with 25 rules, but I thought there might be an easier
way to do it using VBA.


 




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
Outlook 2003 rule for incoming mails hg[_2_] Outlook - General Queries 4 February 7th 08 07:44 PM
Rule print message does not work with encrypted message! LVer Outlook and VBA 3 December 9th 07 05:51 AM
Macro to kick in every time I send a message Jyoti Outlook and VBA 5 May 27th 06 12:04 PM
Incoming Rule only allowing messages to 2 recipients through to 1 pprice Outlook - Installation 0 February 10th 06 12:21 AM
Opening Outlook is kick starting windows messenger? JB Outlook - General Queries 1 January 29th 06 10:54 AM


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


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