Search code examples
vbaemailmove

VBA: Move Mail from Junk to Inbox


I'm trying to move all my junk mail to my inbox by VBA code. Unfortunately i get an '424 object required' error on this line:

    Item.Move (olFLD)

The code works when i try to move mail from inbox to another folder but it doesn't work the other way around when i try to move a mail from junk to the inbox.

Dim OlNS As Outlook.NameSpace
Dim olFLD As Outlook.Folder
Dim olJunk As Outlook.Folder

Sub Junk()

Set OlNS = Outlook.GetNamespace("MAPI")
Set olFLD = OlNS.GetDefaultFolder(olFolderInbox)
Set olJunk = OlNS.GetDefaultFolder(olFolderJunk)

While olJunk.Items.Count <> 0

For Each Item In olJunk.Items

Item.Move (olFLD)

Next

Wend

End Sub

Does anyone have an answer to my problem?

Thanks in advance.


Solution

  • Parenthesis are weird in vba. When you call a function from an object where you're not expecting a return value, you'd leave off the parenthesis.

    Item.Move olFLD
    

    Alternatively, to avoid confusion as to when and when not to use parenthesis, you can preface functions with Call

    Call Item.Move(olFLD)
    

    Read more: What are the rules governing usage of brackets in VBA function calls?