In an existing mailbox folder hierarchy, there is a folder containing subfolders named "01", "05", "06", etc. Notice that the numbers are not consecutive: "02", "03", "04" are missing in this example. In my VBA code, I need to check whether a folder named, say, "02" exists and if not, create it.
I currently have (stripped to bare minimum):
Dim NameStr as String
NameStr="02"
On Error Resume Next
Set NewSubFolder = ContainerFolder.folders(NameStr)
On Error GoTo 0
If NewSubFolder Is Nothing Then
Set NewSubFolder = ContainerFolder.folders.Add(NameStr)
End If
As long as NameStr contains an alphanumeric string, or a numbers-only string with value larger than the count of items if ContainerFolder (such as "2020"), everything works. However, if NameStr is set to "02" (or other value low enough to be interpeted as sequence number of an existing subfolder), the first set statement, instead of failing and returning Nothing, returns a pointer to the third folder (counting from zero, 02 corresponds to 3rd item) in the container folder. That would be "06" in the example above. No folder named "02" will be created.
Apparently the VBA interpreter "kindly" converts the string "02" to integer 2, and then returns pointer to the third folder. How do I prevent this behaviour? How do I force Outlook to check for existence of a folder with only digits in its name?
Set NewSubFolder = ContainerFolder.Folders.Item(NameStr)
? – Vincent GNothing
if no match). – Tim Williams