0

I would like to create a code if there is a folder in outlook than just pass and run the rest of the code else create the folder than just run the rest of the code

If objFolder Is Nothing Then
    objFolder = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
    objFolder = objFolder.Folders.Add("test", Outlook.OlDefaultFolders.olFolderInbox)
End If
braX
  • 11,506
  • 5
  • 20
  • 33
G. Micskei
  • 43
  • 1
  • 11

3 Answers3

0

https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/object-required-error-424

You're not using Set to assign objFolder

In VBA Set is required when assigning objects to a variable.

See: What does the keyword Set actually do in VBA?

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
0

Typically you should use the following construction:

If obj Is Nothing Then
    ' need to initialize obj: '
    Set obj = ...
Else
    ' obj already set / initialized. '
End If

So, use the following code instead:

Sub MoveItems()

 Dim myNameSpace As Outlook.NameSpace
 Dim myInbox As Outlook.Folder
 Dim myDestFolder As Outlook.Folder
 Dim myitems As Outlook.Items
 Dim myitem As Object
 Dim objApp As Outlook.Application
 Dim objFolder As Outlook.Folder
 Dim objTestFolder As Outlook.Folder

 Set myNameSpace = Application.GetNamespace("MAPI")
 Set myInbox = myNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

    On Error Resume Next
    Set objTestFolder = myInbox.Folders("test")
    If objTestFolder Is Nothing Then
        Set myDestFolder = myInbox.Folders.Add("test", Outlook.OlDefaultFolders.olFolderInbox)
    End If
    On Error GoTo 0

 Set oDeletedItems = Application.Session.GetDefaultFolder(olFolderDeletedItems)
 Set myitems = oDeletedItems.Items

 Debug.Print myitems.Count

 For I = myitems.Count To 1 Step -1
    myitems.Item(I).Move myDestFolder

 Next

End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks all of you. I dont know whats happening but its still not working got the same error. jsut added my full code maybe you can see what I cant ... – G. Micskei Jul 11 '19 at 17:33
  • pasted it strange – G. Micskei Jul 11 '19 at 17:42
  • You didn't initialize the `Namespace` object. I will correct my post in a moment. – Eugene Astafiev Jul 11 '19 at 17:50
  • its working but I can run only once. I mean if i paste it and run the first time its doing what it should but second time I got an error 440 VB items can not be moved. If I delete the folder (test) and re run the code its workign again. Can you pelase cehck it ? Thanks – G. Micskei Jul 12 '19 at 08:58
  • managed to solve this, had to separate all the "steps" that did the trick Big thanks all of you – G. Micskei Jul 12 '19 at 13:55
0

As already mentioned, you need to use the keyword Set to assign an object to a variable. The following code will first check whether the folder already exists. If not, it creates one.

Dim objFolder As Outlook.Folder
Dim objTestFolder As Outlook.Folder

Set objFolder = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

On Error Resume Next
Set objTestFolder = objFolder.Folders("test")
If objTestFolder Is Nothing Then
    Set objTestFolder = objFolder.Folders.Add("test", Outlook.OlDefaultFolders.olFolderInbox)
End If
On Error GoTo 0
Domenic
  • 7,844
  • 2
  • 9
  • 17