• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Outlook script to add line to text file

How can I get Outlook 2003 and possibly Outlook 2000 script or macro to add 1 line of txt to an external text file when a message arrives?  
0
JPFN
Asked:
JPFN
  • 4
  • 2
1 Solution
 
David LeeCommented:
Hi JPFN,

This macro will add a line of text to an external file with each message arrival.  Right now it writes out the date & time and the subject of the newly arrived message.  Of course you can change it to write out most anything you want.  To use this macro, do the following:
1.  Start Outlook
2.  Click Tools->Macro->Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
4.  Copy the code below and paste it into the right-hand pane of the VB Editor window
5.  Make any changes to the code
6.  Click the diskette icon on the toolbar to save the changes
7.  Close the VB Editor
8.  Click Tools->Macro->Security
9.  Set the Security Level to Medium
10.  Close Outlook
11.  Start Outlook
12.  Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run.  Say yes.


'Macro Begins Here
Private WithEvents objInboxItems As Items

Private Sub Application_Startup()
    Dim objNS As NameSpace
    Set objNS = Application.GetNamespace("MAPI")
    Set objInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
    Set objNS = Nothing
End Sub

Private Sub Application_Quit()
    Set objInboxItems = Nothing
End Sub

Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
    Dim objFSO As Object, _
        objFile As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Change the path and file name on the next line to those of your file
    Set objFile = objFSO.OpenTextFile("C:\eeTesting\EmailLog.txt", ForAppending, True)
    'Change what's written to the file ont he next line
    objFile.Write Now & " " & Item.Subject & vbCrLf
    objFile.Close
    Set objFile = Nothing
    Set objFSO = Nothing
End Sub
'Macro Ends Here

Cheers!
0
 
JPFNAuthor Commented:
Hi BlueDevilFan
Thanks for that I keep getting a run time error β€˜5’ Invalid procedure call or argument

The debugger flags the following line:
Set objFile = objFSO.OpenTextFile("C:\eeTesting\EmailLog.txt", ForAppending, True)

I have tried creating variations of the folder name with and without the file emailog.txt but get the same error each time.

Thanks again
Paul
0
 
David LeeCommented:
Try changing this

    Set objFile = objFSO.OpenTextFile("C:\eeTesting\EmailLog.txt", ForAppending, True)

to

    Set objFile = objFSO.OpenTextFile("C:\eeTesting\EmailLog.txt", 8, True)
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
JPFNAuthor Commented:
Hi BlueDevilFan
That worked thanks again.
Paul
0
 
David LeeCommented:
Great.  Glad I could help out.
0
 
lucasd1973Commented:
I am looking for almost the same thing but for calendar events. I wish to be able to call this script with Rules and Alerts.
0
 
David LeeCommented:
Hi, lucasd1973.

Would you mind opening a new question for this?
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now