Help on "Run a Script" rule in Outlook 2010

Neosco

New Member
Joined
Feb 8, 2014
Messages
7
Hello,

I have a script that i need to execute every time I receive an email in my outlook account. I have added the script in this outlook session. The script runs well manually but when i am trying to tie it up with a rule "run a Script" I cannot see anything in the Script window it appears blank. Please help me know where is the problem. The code I am trying to use is as follows:
Code:
Option Explicit
Public Sub SaveMessageAsMsg()
  Dim oMail As Outlook.MailItem
  Dim objItem As Object
  Dim sPath As String
  Dim dtDate As Date
  Dim sName As String
  Dim enviro As String
 
    enviro = CStr(Environ("USERPROFILE"))
   For Each objItem In ActiveExplorer.Selection
    Set oMail = objItem
 
   sName = oMail.Subject
  ReplaceCharsForFileName sName, "_"
 
   dtDate = oMail.ReceivedTime
  sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
    vbUseSystem) & Format(dtDate, "-hhnnss", _
    vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg"
 
     sPath = enviro & "\Documents\"
  Debug.Print sPath & sName
  oMail.SaveAs sPath & sName, olMSG
  Next
End Sub
 
Private Sub ReplaceCharsForFileName(sName As String, _
  sChr As String _
)
  sName = Replace(sName, "/", sChr)
   sName = Replace(sName, "\", sChr)
  sName = Replace(sName, ":", sChr)
  sName = Replace(sName, "?", sChr)
   sName = Replace(sName, Chr(34), sChr)
  sName = Replace(sName, "<", sChr)
  sName = Replace(sName, ">", sChr)
  sName = Replace(sName, "|", sChr)
End Sub


If there is problem with this script please help me with the modified Script. I also tried to use Newevent as application and Newmail but it is also not working.
 
Last edited by a moderator:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
In order to be able to assign this to a macro you would need to change the code to look something like this (tested)

Code:
Public Sub SaveMessageAsMsg(oMail As Outlook.MailItem)
Dim objItem As Object
Dim sPath As String
Dim dtDate As Date
Dim sName As String
Dim enviro As String

enviro = CStr(Environ("USERPROFILE"))

sName = oMail.Subject
ReplaceCharsForFileName sName, "_"

dtDate = oMail.ReceivedTime
sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
vbUseSystem) & Format(dtDate, "-hhnnss", _
vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg"

sPath = enviro & "\Documents\"
Debug.Print sPath & sName
oMail.SaveAs sPath & sName, olMSG

End Sub
Hope this helps

Simon
 
Upvote 0
Hi Simon,

Thanks for taking your time to help me.

I tried using the code that you suggested. When I store the script in this outlook session and try to run it a blank macro dialog box appears where I have to create a Macro name for the Script. When I create a name it opens up a module window. I even tried pasting my script into module window but when i try to run it again blank dialog box pops up to name the macro. I believe Public Sub SaveMessageAsMsg() makes the script run but when i use Public Sub SaveMessageAsMsg(oMail As Outlook.MailItem) the script is not running and its making me to name the macro. Also I need to run the Script on a folder which lies in public folders in my outlook account and not in the Inbox. I believe this script will work if i apply the script to run on every email that is received in outlook account. Right?

I will appreciate your help on this.
 
Upvote 0
Hi Simon,

Thanks for taking your time to help me.

I tried using the code that you suggested. When I store the script in this outlook session and try to run it a blank macro dialog box appears where I have to create a Macro name for the Script. When I create a name it opens up a module window. I even tried pasting my script into module window but when i try to run it again blank dialog box pops up to name the macro.
I'm not sure I 100% follow what you are saying, but it appears that you are abandoning the idea of using an Outlook Rule to run a script when you receive emails that meet certain (or any) criteria. Please read this article for background information on creating code that you can use in conjunction with a rule in Outlook 2010.

If you follow these instructions you should have no problems:
  • copy and paste the code I originally posted into any module (ThisOutlookSession or otherwise)
  • create a new rule
  • select the conditions under which you want that rule to run (must be on emails you receive)
  • Under actions, select the checkbox by "run a script"
  • In the box below click on "run a script"
  • On the Select a Script dialogue box choose your script
  • Finish the rest of the setup

Hope this helps

Simon
 
Upvote 0
Hello Simon, I am not abandoning the idea of using rules but my problem i that when I stored your script to thisoutlooksession or in a module it is asking me to name the macro. When I name and create it opens up a new window with start as Public Sub SaveMessageAsMsg() I tried putting your code in this window and hit run but again the below window popsup. I believe it is because of Public Sub SaveMessageAsMsg(oMail As Outlook.MailItem) this beginning that I am getting the error else it was running fine when i hit run.It is not even showing in select script window since it is not working manually. If it does not run manually then it wont run as an email arrives..Please Help
xcreate20.gif.pagespeed.ic.EwJ5_7XGzh.png
 
Last edited:
Upvote 0
I think we are at cross purposes here. Either you want a procedure that runs based on an Outlook Rule or you want to manually execute it. When you call a procedure with a rule you must pass an Outlook Item object to it, hence
Code:
[COLOR=#574123]Public Sub SaveMessageAsMsg(oMail As Outlook.MailItem)[/COLOR]
general practice is to name the variable item rather omail but it doesn't matter. When you try and run this by pressing Alt+F8 you won't see the macro exactly because it requires an object to be passed to it.

It is not even showing in select script window since it is not working manually. If it does not run manually then it wont run as an email arrives

I didn't understand much of the rest of your post but hopefully this explains the above. Figure out what it is you're trying to do (execute manually or by a rule). If it is through a rule then my original code will work, if not then you will need to set oMail somewhere else. Like this for example
Code:
Dim oMail As Object
Dim oFld As Object


Set oFld = Session.PickFolder
Set oMail = oFld.Items.GetLast

Simon
 
Upvote 0
I want a procedure that runs based on an outlook rule. But the code that you suggested is not working. It is not saving the incoming emails to the documents folder. I tried using variations in the rule itself but it didnt work.
 
Upvote 0
The code I originally posted simply replaced the way you were assigning a mailitem to the oMail Object. The code runs fine for me, and in your first post you said that your original code also ran fine. If you have added an Outlook Rule and set it up correctly to run this code (instructions on how to do so I have linked to several times in this thread) then I don't see how the code can now be failing for you. Can you please provide more information about exactly how the "code I suggested is not working"?
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,542
Members
449,169
Latest member
mm424

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top