OL2007 - is there a way to disable the "feature" that cripples mails that are in the "junk" folder?

zdoe

Member
see - i don't want to always have to move the mails elsewhere to scan them, and viewing the messed-up mails there is just not an option. i'm on whitelist-only to the inbox, so i do have to scan spam periodically.

if that folder could be fixed, that would be best/simplest.

if not, i'd be happy with an automated thingy that would move things elsewhere as soon as they arrive, but i've not been able to write a rule that would do this easily and user-controllably - basically because the whitelist is not easily user-manageable.

i used to do this with spampal and NOT using outlook's spam (dys)functionality. but spampal is at retirement age and quite resource-hungry, so i dropped it a while back after some other app had messed with its transparent proxying and i would've had to spend some time on it.

now - i'm already running a VBA macro at system startup to get the doubled-line-feeds -problem sorted, so was wondering if someone smarter than me would be able to write a simple piece of VBA code that would move anything from junk folder to a user-designated real folder?

thoughts?
 

zdoe

Member
Why not just disable the junk mail feature?
i do need machine-assisted pre-sorting - otherwise it becomes too laborious to manage the junk.

However, you could use an item add macro to watch the folder and move all mail that hits it.
item add macro - yes, i believe that would be the answer. where could i find a resource on how to do it? or - if it's just a few lines of code, could you perhaps please provide those?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I might have one around here somewhere - see the last macro here - http://www.slipstick.com/outlook/rules/mark-items-read/ - you'll need to use this line in the startup macro:
Set Items = Session.GetDefaultFolder(olFolderjunk).Items

and something like this in the item add macro, assuming the folder is at the same level as the inbox and is called My Stuff. :)

Set Items = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("My Stuff")

More info on using non-default folders is here: http://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/
 

zdoe

Member
finally collected the courage to try this out - i'm a nooooob! have copy pasted your suggested code and renamed folders wo what i think they are on my box.

(and i finally got my first "hello world" macro lifted from the net to run... - so not all is hopless)

i get a compile error, that points to the line 2, with the code:
Private WithEvents Items As Outlook.Items

Sub JunkToZunk()
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Set Items = Session.GetDefaultFolder(olFolderJunk).Items
' Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
Set Items = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("xPort")
' Set MovePst = GetFolderPath("NewPST\Sent Items")
Set MovePst = GetFolderPath("\xPort\Zunk")
Item.UnRead = False
Item.Move MovePst
End Sub

what might i have done wrong?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
at the very least, the lines are in the wrong order -

The Application_Startup macro tells outlook to watch the junk mail folder. Because you are using a folder in your mailbox, you don't need to use GetFolderPath. If xPort is at the same level as the Inbox, this code should work.

Code:
Private WithEvents Items As Outlook.Items

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

Private Sub Items_ItemAdd(ByVal Item As Object)
Set MovePst = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("xPort")
Item.UnRead = False
Item.Move MovePst
End Sub
 

zdoe

Member
tried - doesn't seem to do anything, but now i don't now get a compile-error.

should the code be in:
Module; or
ClassModule; or
Application

& also:

the target folder is on the top level of a pst - shouldn't it be pointing to that pst - e.g. - \xPort\Zunk

again apologies for my noobiness - this is the first time i'm trying to do something with VBA.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
it needs to be in ThisOutlookSession, private withevents and application_startup belong at the top.

is xPort a pst or a folder within a data file?

The code in my sample assumes it's a folder in the folder tree like this:
zdoe@example.com
-Inbox
-Calendar
-contacts
-Deleted Items
-xPort


If it's a separate pst, and xPort is the display name in the folder list, then you need to use
Set MovePst = GetFolderPath("xPort\Zunk") and get the GetFolderPath function.

zdoe@example.com
-Inbox
-Calendar
-contacts
-Deleted Items

xPort
- More
-Zunk
-Deleted Items
 

zdoe

Member
it needs to be in ThisOutlookSession, private withevents and application_startup belong at the top.

is xPort a pst or a folder within a data file? If it's a separate pst, and xPort is the display name in the folder list, then you need to use

Set MovePst = GetFolderPath("xPort\Zunk") and get the GetFolderPath function.

zdoe@example.com
-Inbox
-Calendar
-contacts
-Deleted Items

xPort
- More
-Zunk
-Deleted Items
yes, it's a separate PST called xPort on the OL folder tree. so below is what i got now - seemingly still not running.

OLmacroCode.jpg
 

zdoe

Member
i did NOT have GetFolderPath on the last go. now i do, as below.

if i put it in the top of the page i get a compile error. in the bottom or in a separate module no compile errors. but the VBA is seemingly still not doing anything.

-------------------ThisOutlookSession:

Private WithEvents Items As Outlook.Items

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

Private Sub Items_ItemAdd(ByVal Item As Object)
' Set MovePst = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("\xPort\Zunk")
Set MovePst = GetFolderPath("\xPort\Zunk")
Item.UnRead = False
Item.Move MovePst
End Sub

Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim FoldersArray As Variant
Dim i As Integer

On Error GoTo GetFolderPath_Error
If Left(FolderPath, 2) = "\\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "\")
Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
If Not oFolder Is Nothing Then
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = oFolder.Folders
Set oFolder = SubFolders.Item(FoldersArray(i))
If oFolder Is Nothing Then
Set GetFolderPath = Nothing
End If
Next
End If
'Return the oFolder
Set GetFolderPath = oFolder
Exit Function

GetFolderPath_Error:
Set GetFolderPath = Nothing
Exit Function
End Function
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Did you restart outlook or click in the application startup macro and click Run? Is macro security set to low?
 

zdoe

Member
security = no macro security
nothing appears on the macro list - for this code, or otherwise. when i was testing "hello world" it was on the macro list.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Private subs aren't listed under macros - neither are ones that are triggered automatically. This is an application startup that watches for new messages to land in a folder so they won't be listed.

This: \xPort\Zunk should be: xPort\Zunk
 

zdoe

Member
i suspected that about start-up macros...

fixed xPort\Zunk /restarted outlook - still no jive:

Private WithEvents Items As Outlook.Items

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

Private Sub Items_ItemAdd(ByVal Item As Object)
' Set MovePst = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("xPort\Zunk")
Set MovePst = GetFolderPath("xPort\Zunk")
Item.UnRead = False
Item.Move MovePst
End Sub

Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim FoldersArray As Variant
Dim i As Integer

On Error GoTo GetFolderPath_Error
If Left(FolderPath, 2) = "\\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "\")
Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
If Not oFolder Is Nothing Then
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = oFolder.Folders
Set oFolder = SubFolders.Item(FoldersArray(i))
If oFolder Is Nothing Then
Set GetFolderPath = Nothing
End If
Next
End If
'Return the oFolder
Set GetFolderPath = oFolder
Exit Function

GetFolderPath_Error:
Set GetFolderPath = Nothing
Exit Function
End Function
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Add this to the bottom of the thisoutlooksession and select a message and click run. does it move the message?

Code:
Public Sub ManualTest()
    Dim objMail As Object
    Set objMail = Application.ActiveExplorer.Selection.Item(1)
    Items_ItemAdd objMail
Set objMail = Nothing
End Sub
Another options is to add the following as the first line in the itemadd macro. Every time it gets called, it will run. You can add it to the startup macro too.
msgbox "Macro is working"

Are any of the lines red? blue, green, and black are good. Red bad. :)
 

zdoe

Member
no red lines - other colors prevail. ;-)

the new code moves the selected message, indeed. so it seems that for some reason the code doesn't run on startup, or fails to detect new mails that landed in the junk folder. the "itemadd is running" message box pops up as well, requiring a click.

another curious thing is that when i'm resuming from sleep or switching displays i get a full screen view of the VBA editor even if i didn't start it for a while.
-------------
Private WithEvents Items As Outlook.Items

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

Private Sub Items_ItemAdd(ByVal Item As Object)
' Set MovePst = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("xPort\Zunk")
MsgBox "ItemAdd is running"
Set MovePst = GetFolderPath("xPort\Zunk")
Item.UnRead = False
Item.Move MovePst
End Sub

Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim FoldersArray As Variant
Dim i As Integer

On Error GoTo GetFolderPath_Error
If Left(FolderPath, 2) = "\\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "\")
Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
If Not oFolder Is Nothing Then
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = oFolder.Folders
Set oFolder = SubFolders.Item(FoldersArray(i))
If oFolder Is Nothing Then
Set GetFolderPath = Nothing
End If
Next
End If
'Return the oFolder
Set GetFolderPath = oFolder
Exit Function

GetFolderPath_Error:
Set GetFolderPath = Nothing
Exit Function
End Function

Public Sub ManualTest()
Dim objMail As Object
Set objMail = Application.ActiveExplorer.Selection.Item(1)
Items_ItemAdd objMail
Set objMail = Nothing
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
another curious thing is that when i'm resuming from sleep or switching displays i get a full screen view of the VBA editor even if i didn't start it for a while.
This is a bug - I haven't figured out why it does it, but i see it occasionally.

I'll test the code tomorrow morning and see if i can figure out why it's failing for you.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
one question - is the junk folder we're watching in the default data file?

Actually, another way to test it: move mail into the junk mail folder or select mail that is there and copy and paste... that way you can test the item add part.

I probably should have recorded this using Outlook 2007 (it works in all newer versions) but i quickly glanced at the thread and didn't see you mention the version - I noticed the subject after I had it posted on youtube (and didn't want to redo it).

 

zdoe

Member
wow ! a video ! thank you.

Junk folder IS in the default data file - courtesy Bill Gates.

and my folders are as you'd (re)created them - listed at the tail of this.

so - as mentioned before, AddItem does move the email that's selected to the right target folder. but the other macro/subroutine either fails to start on startup, or fails to notice the incoming mails - this whether mail-in-question is copied to Junk E-mail or copy / pasted within it.

list of folders by some VBS utility - notable, perhaps, that "Junk E-mail" doesn't appear on the list.
1/26/2015 1:40:55 AM: Selected Archive Name: Inbox
1/26/2015 1:40:55 AM: Total Number of Sub Folders: 0
1/26/2015 1:41:16 AM: Selected Archive Name: xPort
1/26/2015 2:01:16 AM: Total Number of Sub Folders: 5
1/26/2015 2:01:16 AM: \\xPort\Deleted Items
1/26/2015 2:01:16 AM: \\xPort\Zunk
1/26/2015 2:01:16 AM: \\xPort\xContacts
1/26/2015 2:01:16 AM: \\xPort\SiteStats
1/26/2015 2:01:16 AM: \\xPort\Dr-N Notifications
 
Top