0

I'm trying to move an email in my Outlook 2010 inbox to a sub-folder of a folder (called "Indidents").

The move is being performed by a VBA routine which is passed a sub-folder name (a problem incident reference) and a reference to the email.

If the sub-folder does not exists, it is to be created.

The move works if I have to create the folder, but if the sub-folder already exists then the move fails.

Here is my code:

Sub AddIncidentFolder(incident, ByRef email As Outlook.mailItem)

 incidentDir = "Incidents"

 Dim myNameSpace As Outlook.NameSpace
 Dim inbox As Outlook.Folder
 ' Dim incidents As Outlook.Folder
 Dim incidentSubFolder As Outlook.Folder
 
 Set myNameSpace = Application.GetNamespace("MAPI")
 Set inbox = myNameSpace.GetDefaultFolder(olFolderInbox)
  
 MsgBox "Selecting " & incidentDir & " folder"
 Set incidents = inbox.Folders.item(incidentDir)

 MsgBox "Selecting " & incident & " sub-folder"
 
On Error GoTo addSubFailed
 Set incidentSubFolder = incidents.Folders.Add(incident)
 MsgBox "Incident sub-folder set to " & incidentSubFolder
 
 On Error GoTo MoveError
 email.Move incidentSubFolder
 
 Exit Sub

addSubFailed:
 MsgBox "Error Creating 'Incident' folder " & incident
 MsgBox "Error number: " & Err.Number _
            & " " & Err.Description
 Set incidentSubFolder = incidents.Folders.item(incident)
  MsgBox "folder add result was " & incidentSubFolder
 Resume Next
 
MoveError:
MsgBox "Move of email failed"
Resume Next
  
End Sub

So the idea is that if the 'Add Folder' fails then I assume it exists so I just select the folder in addSubFailed.

The first 2 MsgBox statements in addSubFailed fire, but the third one doesn't, so I assume that the 'Set' for the sub-folder is causing a further error.

I'm pretty new to VBA but OK with objects, properties etc. The code came from other SO answers and MS docs and I can't see anything obviously wrong.

EDIT The 'incident' variable which I thought was a string is actually a Regex match object, from this calling code:

For Each Match In irMatches
  'MsgBox "Match is " & Match & ", value is " & Match.Value
  Call addToCategory(Match, email)
  Call AddIncidentFolder(Match, email)
Next

If I change the addSubFailed routine to:

addSubFailed:
 MsgBox "Error Creating 'Incident' folder " & incident
 MsgBox "Error number: " & Err.Number _
            & " " & Err.Description
 MsgBox "Incident is " & incident
 'Set incidentSubFolder = incidents.Folders(incident)
  Set incidentSubFolder = incidents.Folders("INC000001509771")
  MsgBox "folder is " & incidentSubFolder
 Resume Next

then it works, so I have a type-mismatch I think. Adding `As String' to the 'incident' parm gives me a type mis-match run-time error.

Thanks in advance for any help.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • Have you tried to insert a breakpoint and checking by either writing `stop` where you want to debug or adding a red dot in the left side of your VBA code and moving line by line with `F8` in order to see how your code moves after the error fires? These debugging features should be the first check when something doesn't execute as expected. – Oliver Jul 24 '19 at 11:15
  • Thanks Oliver - let me try that. I'm not very familiar with the VBA editor. If you see my edit, I also need to see how to inspect properties & objects to see what type they are. – Steve Ives Jul 24 '19 at 11:19
  • *"the move fails"* - Please include the exact error message. – Tomalak Jul 24 '19 at 11:22
  • @Tomalak - Err.number is -2147352567, Err.Description is "Cannot move the item", but I think it's down to my 'incidents' variable being a regex 'Match' object and not the 'value' property from that object... – Steve Ives Jul 24 '19 at 11:35

1 Answers1

1

The answer appears to be changing the calling code to :

For Each Match In irMatches
  Call addToCategory(Match, email)
  Call AddIncidentFolder(Match.Value, email)
Next

whilst the older code to assign the email category is happy being passed a match object, as is the Folders.Add method, switching to that folder doesn't work when passed a match object and required the .value property of the match.

The complete code is now (without all that annoying debugging MsgBox stuff)):

Sub AddIncidentFolder(incident, ByRef email As Outlook.mailItem)

 incidentDir = "Incidents"

 Dim myNameSpace As Outlook.NameSpace

 Set myNameSpace = Application.GetNamespace("MAPI")
 Set inbox = myNameSpace.GetDefaultFolder(olFolderInbox)

 Set incidents = inbox.Folders.item(incidentDir)

On Error GoTo addSubFailed
 Set incidentSubFolder = incidents.Folders.Add(incident)

 On Error GoTo MoveError
 email.Move incidentSubFolder

 Exit Sub

addSubFailed:
 Set incidentSubFolder = incidents.Folders(incident)
 Resume Next

MoveError:
MsgBox "Move of email failed"
 MsgBox "Error number: " & Err.Number _
            & " " & Err.Description
Resume Next

End Sub
Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • 1
    Side note `Debug.Print` is way less annoying than `MsgBox` – Tomalak Jul 24 '19 at 12:03
  • Thanks Tomalak - still new to VBA. If there are any other obvious improvement to my code, then happy to have them pointed out. Still a bit vague on when I need `Dim` (for explicitly setting the type of a variable?) and `Set` – Steve Ives Jul 24 '19 at 12:19
  • 1
    `Dim` is for declaring a variable. The `Set` and the (entirely optional) `Let` keywords are for assigning a value to a variable. [Also see](https://stackoverflow.com/q/349613/18771). Go to your VBA IDE Optinons and enable "Require Variable Declaration" and disable "Auto Syntax Check". Add `Option Explicit` as the first line to all the modules that do not already have it. Thank me later. ;) – Tomalak Jul 24 '19 at 12:27