Run Time Error 438

Evil Red Smurf

New Member
Joined
Dec 19, 2013
Messages
3
Hi,

I use VBA quite a lot but I wouldn't say I'm an expert - I have a limited "vocabulary" and work around that with help from MrExcel.com :)

I have an outlook vba macro that I've been using for some time to help file the items in my inbox and it worked fine, however the other day it started coming up with a Run Time Error 438 in the code below. Basically what the macro does is look at each item in my inbox and if the item is marked as complete (with a tick) AND it's received more than 5 days ago then it will file the item in 1 or more folder depending on the categories assigned to the item. The code is as follows(note - I've removed a big chunk from the middle to keep it short, but it's basically the If statement duplicated many times):

HTML:
Sub MoveToFolder()
Dim ns As Outlook.NameSpace
Dim objItem As Object
Dim FolderInbox As Folder
Dim MyItem As Outlook.MailItem
Dim cMails As Collection

Set ns = Application.GetNamespace("MAPI")Set FolderInbox = ns.GetDefaultFolder(olFolderInbox)Set cMails = New Collection

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

For Each objItem In FolderInbox.Items
If objItem.FlagStatus = 1 And objItem.ReceivedTime < Now - 5 Then                 '''''ERROR IS HERE'''''''''''

     If InStr(objItem.Categories, "ACC") > 0 Then                                            '''          
     Set MyItem = objItem.Copy                                                               '''    This section is duplicated many times with different categories          
     MyItem.Move FolderInbox.Folders("ACC")                                                  '''    being copied to different folders
     cMails.Add objItem.EntryID                                                              '''     
     Else                                                                                    '''
     End If                                                                                  '''    

     objItem.Delete

Else
End If 
Next

On Error Resume Next
Do While cMails.Count > 0
Set MyItem = ns.GetItemFromID(cMails(1))

If Not MyItem Is Nothing Then     
MyItem.Delete
End If

cMails.Remove (1)
Loop
End Sub

As I said, the macro worked fine until just the other day and now it comes up with an error everytime the macro in called.

Any help is greatly appreciated.

Many thanks in advance

Evil Red Smurf
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I suspect it is not actually an email. Try this version:
Code:
Sub MoveToFolder()
   Dim ns                          As Outlook.NameSpace
   Dim objItem                     As Object
   Dim FolderInbox                 As Folder
   Dim MyItem                      As Outlook.MailItem
   Dim cMails                      As Collection

   Set ns = Application.GetNamespace("MAPI")
   Set FolderInbox = ns.GetDefaultFolder(olFolderInbox)
   Set cMails = New Collection

   ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

   For Each objItem In FolderInbox.Items
      If TypeOf objItem Is MailItem Then
         If objItem.FlagStatus = 1 And objItem.ReceivedTime < Now - 5 Then                 '''''ERROR IS HERE'''''''''''

            If InStr(objItem.Categories, "ACC") > 0 Then                                            '''
               Set MyItem = objItem.Copy                                                               '''    This section is duplicated many times with different categories
               MyItem.Move FolderInbox.Folders("ACC")                                                  '''    being copied to different folders
               cMails.Add objItem.EntryID                                                              '''
            Else                                                                                    '''
            End If                                                                                  '''

            objItem.Delete

         Else
         End If
      End If
   Next

   On Error Resume Next
   Do While cMails.Count > 0
      Set MyItem = ns.GetItemFromID(cMails(1))

      If Not MyItem Is Nothing Then
         MyItem.Delete
      End If

      cMails.Remove (1)
   Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,388
Messages
6,124,648
Members
449,177
Latest member
Sousanna Aristiadou

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top