PDA

View Full Version : Miscellaneous code help



TheAntiGates
07-23-2007, 04:51 PM
My questions follow. I have barely any familiarity with OL objects but I'll try to fake my way through. I'm dangerous. :eek: Thanks for you true experts if you can offer guidance.

I essentially want to walk through all my mail messages, and depending on certain header traits, I'll [add code to] search the text and header, or just header. I'll try not to make too big a mess:Sub BrowseFolder()
Dim objTargetFolder As MAPIFolder, Item
If Application.ActiveExplorer.Selection.Count = 0 Then 'want to be able to go parent.parent
MsgBox "Select a(some) mail message(s) to use this.", vbInformation
Exit Sub
End If
Set Item = Application.ActiveExplorer.Selection.Item(1)
If Not GetFolder(olMailItem, objTargetFolder, Item) Then MsgBox "crap": Exit Sub
End Sub
Function GetFolder(pintFolderType, objFolder As MAPIFolder, oItem)
Dim objMyFolders As Outlook.Folders, objSession As Object, objMessage, Item, objSch As Search
Dim i As Integer, j As Integer
GetFolder = False
Set objMyFolders = oItem.Parent.Parent.Folders 'this gives 21 folders
Set objSession = CreateObject("MAPI.Session") ' Create MAPI session
objSession.Logon "", "", False, False, 0 ' Logon using an existing MAPI session
For i = 1 To objMyFolders.Count
If objMyFolders.Item(i).DefaultItemType = pintFolderType Then
Set objFolder=objMyFolders.Item(i) 'this is one "Deleted" (or something) msg
GetFolder = True
For j = 1 To objFolder.Items.Count
' Get the Outlook item with CDO
Set objMessage=objSession.GetMessage(objFolder.Items(j).EntryID,objFolder.Store ID)
Set objMessage = Nothing
Next j
'Set objSch = Application.AdvancedSearch(Scope:="Inbox", Filter:=strF, _
'SearchSubFolders:=True, Tag:="SubjectSearch")
'Use the AdvancedSearchComplete event to determine when a given search has completed.
End If
Next i
objSession.Logoff ' Close MAPI session
Set objSession = Nothing
If Not GetFolder Then
MsgBox "Problem getting folder", vbCritical, Application.Name
End If
End Function
It looks like (from shft-F9 or Watch window) objFolder.Items(j) has no properties and that you have to do a .GetMessage to do or know anything. Is that true, and if so, isn't that as bad as opening the message? I believe I'd prefer not opening it. *

Actually I'll be fine if I can just restrict to .unread=true, but I don't know how to get there. I guess I want to see the header without having to do .GetMessage.

FYI, I'm aiming for two things: to roll my own search macro since Outlook's supplied one is so sad; and to highlight and read any message header to see the true sender address 'ere opening a message. But right now all I've got is objMessage.Sender which - yep, I fear that it requires an opened message. :banghead:
GIA

* My understanding is that opening an OL email exposes any viruses/Trojans, and unless you have graphics shut off, pings that spammer to assuage him that I'm not only alive and well, but since I'm so doofus as to open spam, my address is 41,000 times more valuable on the open market. Woohoo! Be sure to send me a "Congratulations" popup! 14 times an hour, please!

geekgirlau
07-23-2007, 06:31 PM
Are you running this from within Outlook? If so, you don't need to connect to the session.

I've sanitised the code below, but essentially this loops through unread messages in the Inbox, moves them to a nominated folder and marks them as read. This code goes in the ThisOutlookSession module.


Public Sub CheckEmails()
Dim OLFldr As Outlook.MAPIFolder
Dim OLDestFldr As Outlook.MAPIFolder
Dim OLMail As Outlook.MailItem
Dim strReport(0 To 3) As String
Dim i As Integer

Const cstr_PROCEDURE = "CheckEmails"


On Error GoTo ErrHandler
strReport(0) = "Test Subject 1"
strReport(1) = "Test Subject 2"
strReport(2) = "Test Subject 3"
strReport(3) = "Test Subject 4"

Set OLFldr = Session.GetDefaultFolder(olFolderInbox)

For Each OLMail In OLFldr.Items
' only check unread mail
If OLMail.UnRead Then
For i = 0 To UBound(strReport)
If OLMail.Subject = strReport(i) Then
Set OLDestFldr = OLFldr.Folders("Name of Destination Folder")

' do other stuff here?

' move email and flag as read
With OLMail
.UnRead = False
.Move OLDestFldr
End With

Exit For
End If
Next i
End If
Next OLMail


ExitHere:
On Error Resume Next
Exit Sub

ErrHandler:
MsgBox Err.Description, vbCritical, "Unexpected Error (" & Err.Number & ") in " & _
cstr_PROCEDURE
Resume ExitHere
End Sub

TheAntiGates
07-23-2007, 07:02 PM
Yes, I run this in OL so your advice is noted.

So the [unopened] message items do have properties after all. I seem to have a hard time locating those properties. I wonder why I couldn't see them in the watch window.

I don't seem to be able to get to VBA in OL97 right now so I'll try again later. Thank you.

TheAntiGates
07-24-2007, 07:41 AM
.Subject and .Unread are available, but not .Name and .Address. So the MAPI session object is required for GetMessage if that's what must be used to get .Name. I am open to other "non add-in" routes to that info.

Your use of Outlook.MailItem suggests that my variable Item is such, which leads me to a number of properties I might evaluate, despite the deceptive Watch Window in the IDE (and using a variant was not the culprit, BTW).

If someone knows whether Application.AdvancedSearch can be restricted to unread items, that might be fruitful. However if it has no further capabilities than the user interface search, I prefer to just write one that does. I would start with Instr unless there are better suggestions.

Thanks for taking the time to help with my code.