2

I'm using Outlook 2007. I have no experience with Windows programming or VBA, as all of my background is on Unix systems. I am attempting to have Outlook save each incoming emails as text file, and then a run a python script which will parse this text file.

I found this question which seems to provide some VBA code to accomplish the first task, but I'm not sure how to utilize it. I open the Visual Basic Editor, but have no idea how to proceed from there. I tried saving the text as a module, but when I attempt add "run a script" in an Outlook rule, no scripts are available. What am I doing wrong?

EDIT: Clarification.

Community
  • 1
  • 1
David Y. Stephenson
  • 872
  • 4
  • 22
  • 44

1 Answers1

3

I'm assuming you're familiar with coding at some level, even if not on Windows. You may want to do some general background reading on Outlook VBA; some resources on this would be a Microsoft article, this article from OutlookCode, and so on - there's a ton of resources out there with walkthroughs and examples.

Addressing your specific question: take a look at this Microsoft KB article, which describes how to trigger a script from a rule.

The key to getting started once you've gotten your VBA editor open is to double-click a module on the left, for example ThisOutlookSession (under 'Microsoft Outlook Objects'). That should give you an editor you can paste code into. Bear in mind that (per the above MS page) your procedure must accept a MailItem object, which will be the item that the rule has in hand, so the linked example you gave would have the first couple of lines changed from:

Sub SaveEmail()
    Dim msg As Outlook.MailItem
    ' assume an email is selected
    Set msg = ActiveExplorer.Selection.Item(1)
    ' save as text
    [...]

...to:

Sub SaveEmail(msg As Outlook.MailItem)
    ' save as text
    [...]

Essentially you're being handed a MailItem rather than having to create it and connect it to the selected item in Outlook.

To achieve the second task of 'running a script' on the file, I'm assuming that you want your VBA to make changes to the file after it's been saved? This is pretty straightforward in VBA, and you'll find lots of examples for it. One pretty simple outline is in this answer.

Edit based on comments: to launch an external tool, you can use either the Shell command if you don't need to wait for it to complete, or you can use one of the many Shell-and-wait implementations floating around, for example this popular one. Or, you can use the WScript approach in this answer.

Note that you'll need to ensure Outlook is set to allow macros to run, and you will probably want to sign your code.

Community
  • 1
  • 1
Geoff
  • 8,551
  • 1
  • 43
  • 50
  • "double-click a module on the left" did it! Do you have any suggestions for getting a python script to run after the file has been saved? – David Y. Stephenson Jul 17 '13 at 16:18
  • Great! To run a python script you could either launch the interpreter via command-line, or [convert the script to an EXE](http://www.py2exe.org/) and launch _that_. I'll add an edit regarding launching an external process. – Geoff Jul 17 '13 at 16:25