msoutlook.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

VBA macro not scanning entire Inbox

 
Post new topic   Reply to topic    msoutlook.org Forum Index -> MS Office Outlook
Author Message
Daniel Sommerfeld



Joined: 13 Aug 2007
Posts: 1

PostPosted: Wed Feb 20, 2008 7:22 pm    Post subject: VBA macro not scanning entire Inbox Reply with quote

I have written a macro, originally for in Outlook 2003, that seems to be
affected by our upgrade to Outlook 2007.
The macro searches the subject of all of the messages in the inbox for a
certain string (BCE #), and if it finds the string, it extracts a job number
following the string, and saves it to a matching job number folder on the
network. Some subjects contain multiple instances of the search string.
Some days, the macro works just fine, other days it only scans a few
messages, then says that it has completed successfully.
Any advice or suggestions on what to look for or change? I don't know VBA
and still wonder how I was able to piece this together.

Public Sub SaveProjects()

Dim Inbox As Outlook.MAPIFolder
Dim ns As NameSpace
Dim item As Object
Dim strCheck As String, i As Integer
Dim LogFile
Dim strdate As String
Dim sPath As String

strtoday = Date
strCheck = "\/:*?|"
For i = 1 To Len(strCheck)
strtoday = Replace(strtoday, Mid$(strCheck, i, 1), "-")
Next i

'This section creates the html file on the hard drive
'----------------------------------------------------
Open "c:\savedemail-" & strtoday & ".html" For Append As #1
Print #1, ""
Print #1, ""
Print #1, " bordercolor=#c0c0c0>"
Print #1, ""
Print #1, ""
Print #1, ""
Print #1, "Archived Messages - "
& Now & ""
Print #1, ""
Print #1, ""
Print #1, ""
Print #1, " size=1>Date"
Print #1, " size=1>Path"
Print #1, " size=1>File Name"
Print #1, ""
Close #1

'This section scans the messages in the Inbox for "BCE #" (case insensitive).
'If found, it extracts 5 characters after the # and sets them as the job
number.
'It then scans the network for the job number.
'-------------------------------------------------------------------------------
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
For Each item In Inbox.Items
If InStr(1, item.Subject, "BCE #", 1) > 0 Then
strtemp = Split(item.Subject, "BCE #", , 1)
For n = 0 To UBound(strtemp)
strtemp(n) = Left(strtemp(n), 5)
If IsNumeric(strtemp(n)) Then
strill = "\/:*?""|"
For i = 1 To Len(strill)
item.Subject = Replace(item.Subject, Mid$(strill, i, 1), "-")
Next i
strpar = ")."
For i = 1 To Len(strpar)
strtemp(n) = Replace(strtemp(n), Mid$(strpar, i, 1), "")
Next i
strzero = Mid$(strtemp(n), 1, 1)
If strzero = "0" Then
strtemp(n) = Right(strtemp(n), 2)
End If
strtemp(n) = Trim(strtemp(n))
job = Left(strtemp(n), Len(strtemp(n)) - 3)
sjob = "p:\" & Format(job, "00") & "000s\" & strtemp(n)
semail = sjob & "\email"

'This section saves the message if the job number is found.
'If the file exists, it adds a number to the end until it can be saved.
'This section also adds an entry into the html file for each message that is
saved.
'-----------------------------------------------------------------------
If Not item.FlagStatus = 2 Then
If DirExists(sjob) Then
If DirExists(semail) Then
If FileThere(sjob & "\email\" & item.Subject &
".msg") Then
n = 0
Do
If FileThere(sjob & "\email\" & item.Subject &
"-" & n & ".msg") Then
n = n + 1
Else
Exit Do
End If
Loop
Open "c:\savedemail-" & strtoday & ".html" For
Append As #1
Print #1, " size=1>" & Now & ""
Print #1, " size=1>"; sjob; ""
Print #1, " size=1>"; item.Subject; ""
Close #1
item.SaveAs sjob & "\email\" & item.Subject & "-" &
n & ".msg", olMSG
'item.Delete
Else
Open "c:\savedemail-" & strtoday & ".html" For
Append As #1
Print #1, " size=1>" & Now & ""
Print #1, " size=1>"; sjob; ""
Print #1, " size=1>"; item.Subject; ""
Close #1
item.SaveAs sjob & "\email\" & item.Subject &
".msg", olMSG
'item.Delete
End If
'If the job number is not found, the message is flagged with a green flag.
'If the email folder is not found, the message is flagged with an orange flag.
'-------------------------------------------------------------------------
Else
If item.FlagStatus = 2 Then
item.FlagIcon = 1 'olPurpleFlagIcon
item.Save
Else
item.FlagStatus = 2 'olFlagMarked
item.FlagIcon = 2 'olOrangeFlagIcon
item.Save
End If
End If
Else
If item.FlagStatus = 2 Then
item.FlagIcon = 1 'olPurpleFlagIcon
item.Save
Else
item.FlagStatus = 2 'olFlagMarked
item.FlagIcon = 3 'olGreenFlagIcon
item.Save
End If
End If
End If
End If
Next
If Not item.FlagStatus = 2 Then
item.Delete
End If
End If
Next
Open "c:\savedemail-" & strtoday & ".html" For Append As #1
Print #1, ""
Print #1, ""
Close #1
MsgBox "Done"
End Sub
Function DirExists(ByVal PathName As String) As Boolean
Dim iTemp As Integer
On Error Resume Next
iTemp = GetAttr(PathName)
Select Case Err.Number
Case Is = 0
DirExists = True
Case Else
DirExists = False
End Select
On Error GoTo 0
End Function
Function FileThere(FileName As String) As Boolean
FileThere = (Dir(FileName) > "")
End Function

Archived from group: microsoft>public>office>developer>outlook>vba
Back to top
View user's profile Send private message
Sue Mosher [MVP-Outlook]



Joined: 12 Aug 2007
Posts: 656

PostPosted: Sun Feb 24, 2008 3:14 pm    Post subject: Re: VBA macro not scanning entire Inbox Reply with quote

You should never delete items inside a For Each ... Next loop. Instead, use a down-counting loop:

count = Inbox.Items.Count
For i = count to 1 Step -1
Set item = Inbox.Items(i)
' do stuff to item and if desired, then:
item.Delete
Next

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


"Daniel Sommerfeld" wrote in message @microsoft.com...
>I have written a macro, originally for in Outlook 2003, that seems to be
> affected by our upgrade to Outlook 2007.
> The macro searches the subject of all of the messages in the inbox for a
> certain string (BCE #), and if it finds the string, it extracts a job number
> following the string, and saves it to a matching job number folder on the
> network. Some subjects contain multiple instances of the search string.
> Some days, the macro works just fine, other days it only scans a few
> messages, then says that it has completed successfully.
> Any advice or suggestions on what to look for or change? I don't know VBA
> and still wonder how I was able to piece this together.
>
> Public Sub SaveProjects()
>
> Dim Inbox As Outlook.MAPIFolder
> Dim ns As NameSpace
> Dim item As Object
> Dim strCheck As String, i As Integer
> Dim LogFile
> Dim strdate As String
> Dim sPath As String
>
> strtoday = Date
> strCheck = "\/:*?|"
> For i = 1 To Len(strCheck)
> strtoday = Replace(strtoday, Mid$(strCheck, i, 1), "-")
> Next i
>
> 'This section creates the html file on the hard drive
> '----------------------------------------------------
> Open "c:\savedemail-" & strtoday & ".html" For Append As #1
> Print #1, ""
> Print #1, ""
> Print #1, " > bordercolor=#c0c0c0>"
> Print #1, ""
> Print #1, ""
> Print #1, ""
> Print #1, "Archived Messages - "
> & Now & ""
> Print #1, ""
> Print #1, ""
> Print #1, ""
> Print #1, " > size=1>Date"
> Print #1, " > size=1>Path"
> Print #1, " > size=1>File Name"
> Print #1, ""
> Close #1
>
> 'This section scans the messages in the Inbox for "BCE #" (case insensitive).
> 'If found, it extracts 5 characters after the # and sets them as the job
> number.
> 'It then scans the network for the job number.
> '-------------------------------------------------------------------------------
> Set ns = GetNamespace("MAPI")
> Set Inbox = ns.GetDefaultFolder(olFolderInbox)
> For Each item In Inbox.Items
> If InStr(1, item.Subject, "BCE #", 1) > 0 Then
> strtemp = Split(item.Subject, "BCE #", , 1)
> For n = 0 To UBound(strtemp)
> strtemp(n) = Left(strtemp(n), 5)
> If IsNumeric(strtemp(n)) Then
> strill = "\/:*?""|"
> For i = 1 To Len(strill)
> item.Subject = Replace(item.Subject, Mid$(strill, i, 1), "-")
> Next i
> strpar = ")."
> For i = 1 To Len(strpar)
> strtemp(n) = Replace(strtemp(n), Mid$(strpar, i, 1), "")
> Next i
> strzero = Mid$(strtemp(n), 1, 1)
> If strzero = "0" Then
> strtemp(n) = Right(strtemp(n), 2)
> End If
> strtemp(n) = Trim(strtemp(n))
> job = Left(strtemp(n), Len(strtemp(n)) - 3)
> sjob = "p:\" & Format(job, "00") & "000s\" & strtemp(n)
> semail = sjob & "\email"
>
> 'This section saves the message if the job number is found.
> 'If the file exists, it adds a number to the end until it can be saved.
> 'This section also adds an entry into the html file for each message that is
> saved.
> '-----------------------------------------------------------------------
> If Not item.FlagStatus = 2 Then
> If DirExists(sjob) Then
> If DirExists(semail) Then
> If FileThere(sjob & "\email\" & item.Subject &
> ".msg") Then
> n = 0
> Do
> If FileThere(sjob & "\email\" & item.Subject &
> "-" & n & ".msg") Then
> n = n + 1
> Else
> Exit Do
> End If
> Loop
> Open "c:\savedemail-" & strtoday & ".html" For
> Append As #1
> Print #1, " > size=1>" & Now & ""
> Print #1, " > size=1>"; sjob; ""
> Print #1, " > size=1>"; item.Subject; ""
> Close #1
> item.SaveAs sjob & "\email\" & item.Subject & "-" &
> n & ".msg", olMSG
> 'item.Delete
> Else
> Open "c:\savedemail-" & strtoday & ".html" For
> Append As #1
> Print #1, " > size=1>" & Now & ""
> Print #1, " > size=1>"; sjob; ""
> Print #1, " > size=1>"; item.Subject; ""
> Close #1
> item.SaveAs sjob & "\email\" & item.Subject &
> ".msg", olMSG
> 'item.Delete
> End If
> 'If the job number is not found, the message is flagged with a green flag.
> 'If the email folder is not found, the message is flagged with an orange flag.
> '-------------------------------------------------------------------------
> Else
> If item.FlagStatus = 2 Then
> item.FlagIcon = 1 'olPurpleFlagIcon
> item.Save
> Else
> item.FlagStatus = 2 'olFlagMarked
> item.FlagIcon = 2 'olOrangeFlagIcon
> item.Save
> End If
> End If
> Else
> If item.FlagStatus = 2 Then
> item.FlagIcon = 1 'olPurpleFlagIcon
> item.Save
> Else
> item.FlagStatus = 2 'olFlagMarked
> item.FlagIcon = 3 'olGreenFlagIcon
> item.Save
> End If
> End If
> End If
> End If
> Next
> If Not item.FlagStatus = 2 Then
> item.Delete
> End If
> End If
> Next
> Open "c:\savedemail-" & strtoday & ".html" For Append As #1
> Print #1, ""
> Print #1, ""
> Close #1
> MsgBox "Done"
> End Sub
> Function DirExists(ByVal PathName As String) As Boolean
> Dim iTemp As Integer
> On Error Resume Next
> iTemp = GetAttr(PathName)
> Select Case Err.Number
> Case Is = 0
> DirExists = True
> Case Else
> DirExists = False
> End Select
> On Error GoTo 0
> End Function
> Function FileThere(FileName As String) As Boolean
> FileThere = (Dir(FileName) > "")
> End Function
>

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
The macro cannot be found Hello, Help is needed for the following problem: I wrote some VBA code in Outlook 2003. when I tried to run it from within Outlook by clicking a customized macro button on the toolbar, I received an error message that said "The xxx macro cannot be found".

Macro Execution Pardon my second post, but my first received no response. I am a newbie to vba, but managed to create a macro that works when run manually, I would like the macro to run automatically on the send_item event, or some other event, but I am having trouble

Run a Rule from Macro Is it possible to fire a rule from a macro? Outlook 2003. Bye

Converting VBA Macro to COM-addin? Hi everyone!! Is there a simple way to just convert a few VBA macros for Outlook into an COM-addin, as this seems to be the best way to distribute Macros to other users? And, what do do I need to create a COM-addin? // Johan Ch

Issues with a simple VBA macro I'm trying to write macro to do the following: 1. Decline a selected meeting request silently without sending a response. 2. Delete the meeting request item (So it's not shown in my inbox). My issue is that the item is not removed from the inbox, I can st
Post new topic   Reply to topic    msoutlook.org Forum Index -> MS Office Outlook All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group