Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook macro works with "Mail" but crashes with "Post"

Status
Not open for further replies.

Dawber

Technical User
Jun 29, 2001
86
GB
I have a macro that was kindly donated by a subscriber to this site.

Sub AddDateToSubject()

Dim mItem As MailItem
Dim oFolder As Object

Dim j As Long
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)

For j = myFolder.Items.Count To 1 Step -1
Set mItem = myFolder.Items.Item(j)
mItem.Subject = Format(mItem.SentOn, "YYYYMMDDhhmm") & " " & mItem.Subject
mItem.Save
Next j

Set mItem = Nothing
Set myFolder = Nothing
End Sub


The macro opens each individual post within my Outlook Inbox and adds the date to the subject (ie. “20070124 Original Subject Matter”).

On occasion, I receive messages in my Inbox that are in “Post” or “Delivery Receipt” format, as opposed to “Mail” format, causing the macro to crash. Any assistance in correcting this problem would be greatly appreciated.
 
Hello:

I placed the code within an Error Handling scheme.

On Error GoTo Procedure_Error
' Sub AddDateToSubject()
Dim mItem As MailItem
Dim oFolder As Object
Dim j As Long
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
For j = myFolder.Items.Count To 1 Step -1
Set mItem = myFolder.Items.Item(j)
mItem.Subject = Format(mItem.SentOn, "YYYYMMDDhhmm") & " " & mItem.Subject
mItem.Save
Next j
Set mItem = Nothing
Set myFolder = Nothing
'
Exit_Procedure:
Exit Sub
'
Procedure_Error:
MsgBox "Error " & Err.Number & ": " & Err.Description & ".", vbCritical
Resume Exit_Procedure
End Sub
'
Regards
Mark
 
You may try this:
Sub AddDateToSubject()
Dim mItem As [!]Object[/!]
...
For j = myFolder.Items.Count To 1 Step -1
Set mItem = myFolder.Items.Item(j)
[!]If TypeName(mItem) = "MailItem" Then[/!]
mItem.Subject = Format(mItem.SentOn, "YYYYMMDDhhmm") & " " & mItem.Subject
mItem.Save
[!]End If[/!]
Next j
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks mhartman1 and PHV, the last one was exactly what I was looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top