PDA

View Full Version : Solved: creating a folder and account for an existing one



next
06-19-2012, 11:54 AM
I code works well except for when the folder is already present, it's not moving the email.
Here's how it all works:
1) check email
2) if subject contains two letters followed by a dash and tree digits then create a folder with this code (XX-000) and move the email to it.
The code works well, but if the folder already exists, VBA doesn't move the email because this line Set new_folder = inbox.folders.Add(crew_code) doesn't assign value to new_folder. I created an error handler, that should have fixed the problem, but it's still not working.
here's the complete code:
Sub test()
Dim session As NameSpace
Dim folders As folders
Dim inbox As MAPIFolder
Dim item As Object
Dim reply As Outlook.MailItem
Dim crew_code As String
Dim new_folder As Outlook.folder

Set session = Application.session
Set folders = session.folders
Set inbox = folders("Customers").folders("test")

For Each item In inbox.Items
'check if crew code is referenced in the subject
Set reply = item.reply
crew_code = regex_match(item.Subject, "\D{2}-\d{3}")
If crew_code <> "False" Then
On Error Resume Next
Set new_folder = inbox.folders.Add(crew_code)
item.Move new_folder

With reply
.Subject = "RE: " & item.Subject
.HTMLBody = "<p>Your paperwork has been received and will be processed for payment 30 days " & _
"from today.</p>" & _
"<p>We would also like to ask you to make sure the attachment name(s) " & _
"do not contain characters, such as dashes, commas, dots or any other similar symbols, also " & _
"just as a reminder please be sure that all your invoices reference " & _
"the following information:</p>" & _
"<ul><li>Your crew code</li>" & _
"<li>The work order number(s)</li>" & _
"<li>The store name(s) and number(s)</li>" & _
"<li>The service date(s)</li>" & _
"<li>The amount(s)</li></ul>" & _
"<p>Please feel free to contact us should you have any questions.</p><p>Thank you.</p>"
.Display
'.Send
End With
Else
'handle emails without crew code in the subject line
With reply
.Subject = "RE: " & item.Subject
.HTMLBody = "Your email has been received, however, going forward please include your " & _
"crew code in the subject line of your emails. Thank you."
.Display
'.Send
End With
End If
Next item
'if folder already exists, assign new_folder to an existing folder
FolderExists:
Set new_folder = inbox.folders(crew_code)
Resume Next
End Sub
Function regex_match(value As String, _
pattern As String, _
Optional multiline = False, _
Optional globl = False, _
Optional ignoreCase = False) As String

Dim re As Object, REMatches As Object

Set re = CreateObject("vbscript.regexp")

With re
.multiline = multiline
.Global = globl
.ignoreCase = ignoreCase
.pattern = pattern
End With

If re.test(value) <> 0 Then
Set REMatches = re.Execute(value)
regex_match = REMatches(0)
Else
regex_match = False
End If
End Function

How do I get it to work?:help

next
06-19-2012, 12:05 PM
oops, had wrong On Error call.