Modify VBA to go into Outlook Rule (Run as Script)

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Modify VBA to go into Outlook Rule (Run as Script)

Post by Rudi »

Hi,

I need assistance on some code to incorporate into an Outlook rule.

When a message arrives in the inbox, a rule does the following:
1. Copies it into a specific folder
2. Runs a script file to extract the message body into a text file and then saves the text file with the date in the first line of the text body.

I need help to generate the script file please. I have two macros that I have sourced from other locartions, but I do not know how to get the date in the first line to become the name of the text file.

Any help will be appreciated. TX

Code I have:
Either one can be modified based on my needs...TX

Code: Select all

Sub SaveTXT1()
Dim FileName As Variant
Dim msg As Outlook.MailItem
    ' assume an email is selected
    Set msg = ActiveExplorer.Selection.Item(1)
    FileName = Split(msg.Body, vbCrLf)
    MsgBox FileName
    ' save as text file
    msg.SaveAs "C:\" & FileName & ".txt", olTXT
End Sub

Sub SaveTXT2(Item As Outlook.MailItem)
Dim strExportPath As String, FileName As Variant
    strExportPath = "C:\"
    Dim FileName As String
    'FileName = strExportPath & Replace(Replace(Replace(Now & "_" & Item.EntryID & ".txt", ":", "_"), "-", "_"), " ", "_")    'Create filename and path, replace : with _, - with _ and space with _.
    FileName = Split(Item.Body, vbCrLf)
    Dim FileNum As Integer
    FileNum = FreeFile    ' next file number
    Open FileName For Output As #FileNum    ' creates the file if it doesn't exist
    Print #FileNum, Item.Body    ' write information at the end of the text file
    Close #FileNum    ' close the file
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78465
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Modify VBA to go into Outlook Rule (Run as Script)

Post by HansV »

Split results in a zero-based array. So after

Code: Select all

    FileName = Split(msg.Body, vbCrLf)
FileName is an array. To get the first element, use

Code: Select all

    FileName = Split(msg.Body, vbCrLf)(0)
If the first line of the message contains a date of the form 25/03/2014, it is invalid as a file name because / is forbidden. So you could use

Code: Select all

    FileName = Replace(Split(msg.Body, vbCrLf)(0), "/", "_")
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Modify VBA to go into Outlook Rule (Run as Script)

Post by Rudi »

TX.

It is working now by running this rather simple macro in the script of a rule...

Code: Select all

Sub SaveTXT(msg As Outlook.MailItem)
Dim FileName As String

    FileName = Split(msg.Body, vbCrLf)(0)
    msg.SaveAs "C:\" & FileName & ".txt", olTXT

End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.