Writing small file for each received Outlook 2003 email message

  • Thread starter Thread starter a
  • Start date Start date
A

a

totally new to outlook 2003 and VBA, so please be gentle

yesterday, I added this short piece of code via Tools, Macros, Visual
Basic Editor

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Sub CustomMailMessageRule(Item As Outlook.MailItem)

logfile = "C:\Temp\email.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set fsOut = fso.OpenTextFile(logfile, ForAppending, True)

outputString = "Mail message arrived: " & Item.Subject
fsOut.WriteLine outputString

fsOut.Close

Set fso = Nothing
Set fsOut = Nothing

End Sub

I then created a rule that for each email on this machine, run script
which I called Sample1

To my surprise, it worked fine and wrote in append mode a line for
each received email.

Today, it does not and I can't figure out why it stopped so I removed
the rule, removed the code and added it back in (closed and opened
OL2003 couple times), but it just won't run any more.

What may I have broken? Other than that :) OL2003 runs fine
 
a said:
totally new to outlook 2003 and VBA, so please be gentle
Today, it does not and I can't figure out why it stopped so I removed
the rule, removed the code and added it back in (closed and opened
OL2003 couple times), but it just won't run any more.

What may I have broken? Other than that :) OL2003 runs fine

try this:

Close/Exit OL2003
Start OL2003
Press Alt-F11 which brings up the VBA editor
Close the editor

It will now work

Note that it may start prompting you for allowing it to access userid's and
email addresses if you are using
Item.SenderName or Item.SenderEmailAddress and I don't know how to make it
stop asking, especially if you want the machine to automatically keep
writing that information to the file

Perhaps someone else knows how to make it stop asking about "how long to
allow in minutes" access
 
Does other VBA code run? If not, check Help | About Microsoft Outlook | Disabled Items.
 
Sue said:
Does other VBA code run? If not, check Help | About Microsoft Outlook | Disabled Items.

there are no disabled items.

As Zoe predicted, it runs fine now but I am being prompted every time
since I wanted to use the Items.SenderName

It's hopeless, can't even have a simple script run in OL2003 :( due to
security
 
SenderName shouldn't raise a prompt if the code is constructed properly. In a "run a script" rule procedure, that means getting the item with the Namespace.GetItemFromID method:

Sub RunAScriptRuleRoutine(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim msg As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set msg = olNS.GetItemFromID(strID)
' do stuff with msg, e.g.
MsgBox msg.SenderName

Set msg = Nothing
Set olNS = Nothing
End Sub

For Outlook VBA basics, see http://outlookcode.com/article.aspx?id=49ww.outlookcode.com/d/code/zaphtml.htm#ol2002 for another example.
 
Back
Top