Solved

Outlook rule does not always work

Posted on 2013-01-05
2
447 Views
Last Modified: 2013-01-10
I made a code for Outlook 2010 with an Exchange Server at work. I put the script in ThisOutlookSession.
The rule do the following:
Check incoming mail, from address1 or address2, with the words 'word1' or 'word2' in the topic or text, with an attachment, only at this computer, move the email to a subfolder, run script Projekt1.ThisOutlookSession.SaveAllAttachments and mark it as read.

The code save the attachment as a picture, with the name depending on which time it arrive to Outlook. Then it is printed with irfanview.
The code is here below.

The rule and code works fine most of the time, but sometimes it fails. This happen mostly in the middle of the night, so it is hard to know what happen. When it fails it becomes deactivated.

What can be wrong?


Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub SaveAllAttachments(objitem As MailItem)

    Dim objAttachments As Outlook.Attachments
   Dim strName, strLocation As String
   Dim dblCount, dblLoop As Double

    strLocation = "N:\KARTOR\ANALYS\" ' Ändra till analyskartorkatalogen
   On Error GoTo ExitSub
    If objitem.Class = olMail Then
       Set objAttachments = objitem.Attachments
       dblCount = objAttachments.Count
       If dblCount <= 0 Then
         GoTo 100
       End If

       For dblLoop = 1 To dblCount
                 ' strID = Format(Time, "hh")           'Get the current Time in hours
                   strID = Format(objitem.SentOn, "hh")  'Get the time the mail was sent on
                   'strID = strID & " at " & Format(Time, "hh`mm AMPM") 'Append the Time
                   If strID > 0 And strID <= 6 Then
                     filnamn = "00"
                   End If
                   If strID > 6 And strID <= 12 Then
                     filnamn = "06"
                   End If
                   If strID > 12 And strID <= 18 Then
                     filnamn = "12"
                   End If
                   If strID > 18 Or strID = 0 Then
                     filnamn = "18"
                   End If
 
                  strName = objAttachments.Item(dblLoop).FileName 'Get attachment name
                   strExt = Right$(strName, 4)                     'Store file Extension
                   strName = Left$(strName, Len(strName) - 4)      'Remove file Extension
                   ' strName = strName & strID & strExt              'Reattach Extension
                   ' Tell the script where to save it and
                   ' what to call it
                   strName = strLocation & filnamn & ".jpg"                'Put it all together

                    ' Save the attachment as a file.
                   objAttachments.Item(dblLoop).SaveAsFile strName

                    strCommand = "C:\program files\irfanview\i_view32.exe " & strName & " /print=\\lpcluster4\printer-st1"

                    Dim myApp As Double
                   myApp = Shell(strCommand, vbHide)
        Next dblLoop
       ' objitem.Delete
   End If

100
ExitSub:
   Set objAttachments = Nothing
   Set objOutlook = Nothing
   End Sub
0
Comment
Question by:anderswen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
Andrej Pirman earned 500 total points
ID: 38746657
Don't know about the code, but if it fails usually in the middle of the night, it might be that some of used mechanisms are locked due to BACKUP.

I'd put some debug logging into the script:
- upon execution, write TIME into log file
- then each step, write a line to log file

When next time it will wail, you'll have time and steps in log. Examine Windows Event logs (application and system events) and compare times of failure. You might conclude something useful out of it.
0
 

Author Closing Comment

by:anderswen
ID: 38764180
Thanks, I have done this, and I have also changed the directory it is saved to from the network server to the local disk, and it is transferred later on by a scheduled task to the network server. It has not gone wrong so far. I have also put on logging on Outlook so I am eager to see what can be wrong.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question