Email: bknpk@hotmail.com Phone: +972-54-7649119


V

 

ASIC/FPGA Design and Verification Out Source Services

A visual basic script to save an incoming mail in outlook to a text file.

  1. This page shows a simple example how to customize outlook express. My requirements were to save to a disk every incoming mail as simple text file.

  2. So I discuss, in this page, how to create a script and debug it under visual basic using the macro tool of outlook.

  3. First a word on the requirement. I am able to connect from remote to a linux machine at the office and work normally. I can not see mails, however, because I have no ability to see the windows machine at office on which it runs. So all I need is a rule on microsoft outlook, that on every incoming mail will run my script. The script saves the mail with a unique name.

  4. The script is shown below:.


  5. Sub scr_save(MyMail As MailItem)
      Dim strID As String
      Dim objMail As Outlook.MailItem

      strID = MyMail.EntryID
      Set objMail = Application.Session.GetItemFromID(strID)
      objMail.BodyFormat = olFormatPlain


      Dim str_file As String
      Dim str_date As String
      str_file = "Y:\Junk\CPD\mail_new\"


      Dim str_subj As String
      str_subj = Replace(objMail.Subject, "\", "")
      str_subj = Replace(str_subj, "/", "_")
      str_subj = Replace(str_subj, "!", "")
      str_subj = Replace(str_subj, "@", "")
      str_subj = Replace(str_subj, "#", "")
      str_subj = Replace(str_subj, "$", "")
      str_subj = Replace(str_subj, "%", "")
      str_subj = Replace(str_subj, "^", "")
      str_subj = Replace(str_subj, "&", "")
      str_subj = Replace(str_subj, "*", "")
      str_subj = Replace(str_subj, ":", "")
      str_subj = Replace(str_subj, "`", "")
      str_subj = Replace(str_subj, "'", "")
      str_subj = Replace(str_subj, ",", "")
      str_subj = Replace(str_subj, ">", "")
      str_subj = Replace(str_subj, "<", "")
      str_subj = Replace(str_subj, "?", "")
      '" causes outlook to omit the .txt suffix
      str_subj = Replace(str_subj, Oct(42), "")

      If Len(str_subj) > 80 Then
        str_subj = Left(str_subj, 80)
      End If

      str_date = Format(Now(), "hh mm ss")
      str_date = Replace(str_date, " ", "_") '// replace spaces
      objMail.SaveAs str_file & "ML_" & str_date & "_" & str_subj & ".txt", olTXT
    End Sub

  6. A few notes on the script:
    To create action with an outlook rule, a visual basic script has to be defined. This is done with first line starting with Sub.
    Put your own path: Replace my path C:\cygwin\home\pinhask\Private\ with something that suits your system.
    To debug simple issues, I used the print option:

    MsgBox ml.Subject
    MsgBox ml.SenderName

    I have noticed that the script does not work sometimes and finally got some to debug it. It took me a long time, because the error code, which was printed by, outlook was meaningless. It complained about file write permission. The problem, however, was buried somewhere else.
    Actually outlook does not allow some characters in a file name. While some are trivial like path separator \ and : others are not and where found empirically. So it is possible that the script may fail in some cases.
    In order to be on the safe side, I removed some other characters and restricted the file name to more than 80 characters. Some automatic scripts tend to have too long names.
    Also notice, that " (octal 42), is better stripped off as well from the file name.
    I added this code:

    Dim str_subj As String
    str_subj = Replace(objMail.Subject, "\", "")
    str_subj = Replace(str_subj, "/", "_")
    str_subj = Replace(str_subj, "!", "")
    str_subj = Replace(str_subj, "@", "")
    str_subj = Replace(str_subj, "#", "")
    str_subj = Replace(str_subj, "$", "")
    str_subj = Replace(str_subj, "%", "")
    str_subj = Replace(str_subj, "^", "")
    str_subj = Replace(str_subj, "&", "")
    str_subj = Replace(str_subj, "*", "")
    str_subj = Replace(str_subj, ":", "")
    str_subj = Replace(str_subj, "`", "")
    str_subj = Replace(str_subj, "'", "")
    str_subj = Replace(str_subj, ",", "")
    str_subj = Replace(str_subj, "<", "")
    str_subj = Replace(str_subj, ">", "")
    str_subj = Replace(str_subj, "?", "")
    '" causes outlook to omit the .txt suffix
    str_subj = Replace(str_subj, Oct(42), "")

    If Len(str_subj) > 80 Then
      str_subj = Left(str_subj, 80)
    End If

  7. I have also tried to parse the time of received mail, but decided not use it. You can see this work at: visual basic code used for debug.

  8. Last you need to define a rule to outlook.
    Tools
      Rules and Alerts
        Check messages when they arrive
          where my name in the To or CC box
            run a script
    ...
    no excpetion, name the rule and check it to enable its operation.

    Note: The forward of the outlook rule is usually disabled by default due security issues in the mail server. Therefor I needed the script to the job instead.


  9. Now consider you need to save an entire folder. In this example the default InBox folder. For this you need a MACRO.



  10. Sub save_folder()

      Dim str_file As String
      str_file = "Y:\Junk\CPD\mail\"

      Dim ib_ml As Folder
      Set ib_ml = Session.GetDefaultFolder(olFolderInbox)
      Dim ix As Integer
      ix = 0
      For Each ml In ib_ml.Items
        ml.SaveAs str_file & "FL_" & ix & ".txt", olTXT
        ix = ix + 1
      Next
    End Sub
  11. From outlookPress alt-F8 and run the macro.


  12. Another version of the script also takes the folder name from a file script. This involves reading a text file and trimming CR and use it. The text file merely contains one line with the name of the folder:
    Regression_v_manager
    visual basic code to save an entire folder and use text file for outlook name.


Contact me now at:

  ...



Home

visual basic macro for outlook


How to change the default color scheme of gvimdiff.






Search This Site


Feedback This Site




new pages on this site