1

I had a little macro set up with Outlook on another machine but now that I've switched computers I can't get it to work. When I try to run the last Private Sub, it doesn't recognize the name and pulls up the Macro selection box with no options listed.

I dislike having to manually mark emails in the Deleted Items folder as read, especially considering they had the amazing foresight to mark discarded drafts as unread.

Here's the code that used to work:

Dim WithEvents g_OlkFolder As Outlook.Items

Private Sub Application_Quit()
    Set g_OlkFolder = Nothing
End Sub

Private Sub Application_Startup()
    Set g_OlkFolder = Session.GetDefaultFolder(olFolderDeletedItems).Items
End Sub

Private Sub g_OlkFolder_ItemAdd(ByVal Item As Object)
    Item.UnRead = False
    Item.Save
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
RaptorByte
  • 33
  • 6
  • With my Outlook installation, I have one store per email account plus the default store. Each store has its own Deleted Items folder. The default Deleted Items folder is not used. Could this be true for your new computer? Try typing `? Session.GetDefaultFolder(olFolderDeletedItems).Parent.Name` into your Immediate Window. Does it display the name of the correct store? – Tony Dallimore Feb 25 '19 at 19:59
  • Are you expecting `g_OlkFolder_ItemAdd` to appear in a list of macros available for selection? A macro with a parameter can only be called by another macro and will not be available for selection by the user. – Tony Dallimore Feb 25 '19 at 20:02
  • also check your macro security settings, if that's okay then restart your Outlook and then move unread items to deleted folder. – 0m3r Feb 25 '19 at 20:40
  • @TonyDallimore I'm only using one account on outlook so I don't believe there should be more than one Deleted Items store. I don't really expect or care if g_OlkFolder_ItemAdd appears as a macro. I really just want it to run. – RaptorByte Feb 25 '19 at 21:44
  • @0m3r I've enabled macros and restarted several times. Thanks for trying though – RaptorByte Feb 25 '19 at 21:44
  • @RaptorByte try my answer below. – 0m3r Feb 25 '19 at 22:57

4 Answers4

0

Here are a few things to try and check:

  • Put the cursor in the Application_Startup method and press F5. Then go back and try again. If this helps, the initialization has not run, and the g_OlkFolder variable is not set.
  • Put a breakpoint on the Item.UnRead = False line. If it doesn't stop there, your method isn't running.
  • Have you put your code in the ThisOutLookSession module?
Sam
  • 5,424
  • 1
  • 18
  • 33
  • Hi, thanks for the reply. The Run/F5 in Application_Startup didn't work unfortunately. Didn't notice any difference when I put a breakpoint on the Item.Unread line. I've tried it in a normal Module1 and ThisOutlookSession modules. I'm noticing a new issue now where when I attempt to run any section it gives me a "Compile error: expected end of statement" then highlights the g_OlkFolder in the declarations line – RaptorByte Feb 25 '19 at 21:39
  • The compile error is most probably because you can't use that kind of variable declaration in a normal module. – Sam Feb 26 '19 at 04:46
  • Ah ok that particular issue goes away in a Class Module – RaptorByte Feb 26 '19 at 15:32
0

Try using Application.Session property, or use GetNamespace method which I prefer

Example

Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
    Dim olNs As Outlook.NameSpace
    Dim DeletedFolder  As Outlook.MAPIFolder

    Set olNs = Application.GetNamespace("MAPI")
    Set DeletedFolder = olNs.GetDefaultFolder(olFolderDeletedItems)
    Set Items = DeletedFolder.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
    If TypeOf Item Is Outlook.MailItem Then
        ' do something with Item
    End If
End Sub
0m3r
  • 12,286
  • 15
  • 35
  • 71
  • Hi thanks for the response. Unfortunately, this still doesn't solve my issue of it not running at all. Every time I try to run a sub it just wants to create a new macro. – RaptorByte Feb 26 '19 at 15:48
0

Or define olFolderDeletedItems or replace it with 3. See this link for details.

Victor Ivanidze
  • 397
  • 2
  • 7
0

Upon completely throwing away my code and starting from scratch I figured out a much simpler solution than what I was trying. Thanks for all the help anyways guys!

Sub MDAU()
Dim DI As Outlook.Items
Dim MSG As Object
Set DI = Session.GetDefaultFolder(olFolderDeletedItems).Items
Set MSG = Application.CreateItem(olMailItem)
For Each MSG In DI
MSG.UnRead = False
Next
End Sub
RaptorByte
  • 33
  • 6