Share via

VBA script not working---Why?

Anonymous
2023-06-07T19:16:55+00:00

I am truing to set this script to perform the following

on a certian email account i want to move every email in the Trash folder to the Delete Items.

So i create this script to do that action and nothing happens

Can anyone tell me what is wrong what to fix o replace so i can do this simple task

Sub MoveEmailsFromTrashToDeletedItems()

Dim olApp As Outlook.Application 

Dim olNs As Outlook.NameSpace 

Dim olFolder As Outlook.Folder 

Dim olTrashFolder As Outlook.Folder 

Dim olDeletedItemsFolder As Outlook.Folder 

Dim olItems As Outlook.Items 

Dim olItem As Outlook.MailItem 

' Create an instance of Outlook 

Set olApp = New Outlook.Application 

' Get the MAPI namespace 

Set olNs = olApp.GetNamespace("MAPI") 

' Set the reference to the mailbox you want to work with 

Set olFolder = olNs.Folders("******@domain.com").Folders("Deleted Items") 

' Set the reference to the Trash folder 

Set olTrashFolder = olFolder.Parent.Folders("Trash") 

' Set the reference to the Deleted Items folder 

Set olDeletedItemsFolder = olFolder 

' Get the items in the Trash folder 

Set olItems = olTrashFolder.Items 

' Move each email from Trash to Deleted Items 

For Each olItem In olItems 

    olItem.Move olDeletedItemsFolder 

Next olItem 

' Clean up 

Set olItem = Nothing 

Set olItems = Nothing 

Set olDeletedItemsFolder = Nothing 

Set olTrashFolder = Nothing 

Set olFolder = Nothing 

Set olNs = Nothing 

Set olApp = Nothing 

End Sub

Outlook | Windows | Classic Outlook for Windows | For business

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Anonymous
    2023-06-12T20:56:29+00:00

    Philip
    no there is no error.

    What my concern is about, if Outlook is using imap and smtp to read and send what i see on those folder are really a real mail or is an image of what is at the server level. Because if that is the case i would not be able to perform that task.
    now what is interesting is that you can copy and paste from one folder to another and if that is so i should perform the same task or action.

    Correct me if my thought are wrong.

    Was this answer helpful?

    0 comments No comments
  2. Emmanuel Santana 39,635 Reputation points Independent Advisor
    2023-06-07T21:30:29+00:00

    When you ran the script did you see any error message? There is a chance this could be attributed to retention rules?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2023-06-07T21:27:44+00:00

    Hi Phillip and thank you for your help on this matter.

    Don't know why i still have 12 mails on trash and they are still there.

    I am running this locally and once it is operational will run is on another local out of the admin control.

    Was this answer helpful?

    0 comments No comments
  4. Emmanuel Santana 39,635 Reputation points Independent Advisor
    2023-06-07T19:55:32+00:00

    Hello Alex, thank you for reaching out to the Microsoft Community. I am here to help and provide assistance with any questions or concerns you may have.

    I just checked your VBA script and appears to be correct. Once executed, it should move emails from the Trash folder to the Deleted Items folder in Outlook.

    In the event that the script is not running, you may want to consider examining the following aspects, just in case:

    • Reference Correct Email Account: Verify that you have correctly replaced "@.___" in the line Set olFolder = olNs.Folders("___@___.___").Folders("Deleted Items") with the actual email address associated with the account you want to work with.

    • Correct Folder Names: Make sure the folder names "Trash" and "Deleted Items" match the actual folder names in your Outlook mailbox. Folder names are case-sensitive, so ensure that the capitalization is accurate.

    • Run the Macro: After updating the script, you need to run the macro manually. Press Alt + F8 in Outlook to open the "Macro" dialog box, select MoveEmailsFromTrashToDeletedItems, and click "Run."

    You can also try this script:

    Sub MoveEmailsFromTrashToDeletedItems()

    Dim olApp As Outlook.Application
    
    Dim olNs As Outlook.Namespace
    
    Dim olFolder As Outlook.Folder
    
    Dim olTrashFolder As Outlook.Folder
    
    Dim olDeletedItemsFolder As Outlook.Folder
    
    Dim olItem As Object
    
    
    
    ' Create an instance of Outlook
    
    Set olApp = New Outlook.Application
    
    Set olNs = olApp.GetNamespace("MAPI")
    
    
    
    ' Set the reference to the mailbox you want to work with
    
    Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
    
    
    
    ' Set the reference to the Trash folder
    
    Set olTrashFolder = olFolder.Folders("Trash")
    
    
    
    ' Set the reference to the Deleted Items folder
    
    Set olDeletedItemsFolder = olFolder.Folders("Deleted Items")
    
    
    
    ' Move each email from Trash to Deleted Items
    
    For Each olItem In olTrashFolder.Items
    
        If TypeOf olItem Is Outlook.MailItem Then
    
            olItem.Move olDeletedItemsFolder
    
        End If
    
    Next olItem
    
    
    
    ' Clean up
    
    Set olItem = Nothing
    
    Set olDeletedItemsFolder = Nothing
    
    Set olTrashFolder = Nothing
    
    Set olFolder = Nothing
    
    Set olNs = Nothing
    
    Set olApp = Nothing
    

    End Sub

    If the script still does not work, there may be other factors at play. For example, the script could be restricted by security policies or permissions set by your organization's IT department.

    I hope these suggestions help! Let me know if you have any other questions or if there is anything else I can do to assist you.

    Was this answer helpful?

    0 comments No comments