Consulting

Results 1 to 5 of 5

Thread: VBA to move mail from Gmail 'Important' folder in Outlook to Inbox

  1. #1
    VBAX Regular
    Joined
    Aug 2009
    Posts
    24
    Location

    VBA to move mail from Gmail 'Important' folder in Outlook to Inbox

    I have set up Outlook to retrieve all of my gmail. Unfortunately however, most of the emails are going to the 'Important' folder, hidden in the [Gmail] folder.
    I'm trying to write a macro to automatically move all mail from Important to Inbox, and have this code, but am getting an error. My experience in writing code is extremely poor, so I tend to google and then try to modify - which works out badly!!

    My folder structure is (The folders inside the Gmail folder are the first 3)

    Inbox
    >[Gmail]
    Important
    Drafts
    Sent Mail
    ......
    Outbox
    Search Folders

    Can somebody indicate where I am going wrong? Thanks!!!


    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
      Set myNameSpace = Application.GetNamespace("MAPI")
      Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
      Set myItems = myInbox.Items
      Set myDestFolder = myInbox.Folders("Important")
      Set myItems = myInbox.Folders("Inbox").Items
      'Debug.Print myItems.Count
      For i = myItems.Count To 1 Step -1 'Iterates from the end backwards
       myItems.Item(i).Move myDestFolder
      Next
    End Sub
    Last edited by Aussiebear; 03-11-2023 at 11:44 PM. Reason: Added code tags to supplied code

  2. #2
    Help us to help you ... what EXACTLY is the error message?

    On first glance, you have it backwards - you are moving from Inbox TO Important.

  3. #3
    VBAX Regular
    Joined
    Aug 2009
    Posts
    24
    Location
    Thanks Beethoven,

    Have swapped these two lines.

    The code seems to stop at the line Set myDestFolder = myInbox.Folders("Inbox")

    I am getting an error "The attempted operation failed. An object could not be found.

    Could this be due to the Important folder not being a sub-folder of Inbox, but a subfolder of [Gmail]?

    Thanks.

  4. #4
    Quote Originally Posted by riteoh View Post
    Could this be due to the Important folder not being a sub-folder of Inbox, but a subfolder of [Gmail]?
    Sounds reasonable. Drop a breakpoint and look at the data.

  5. #5
    I don't know about VBA but you can configure Gmail to Outlook to move all items from Gmail mailbox to Outlook without any modification.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •