How to use CDO with local folders?

D

deko

I use VBA to loop through and inspect messages in PST folders on a local
machine and populate an Access 2003 table with messages matching various
search criteria. The problem is with encrypted messages - when the loop
tries to inspect an encrypted message, I get Error13: Type Mismatch. What I
want to do identify encrypted/digitally signed messages with code so I can
process these differently (I assume there is no way to inspect things such
as sender and recipent on encrypted/signed messages).

I found a KB article (194623) that shows how to use CDO to inspect encrypted
messages stored on an Exchange server, but how do I do this on local PST
folders? I've included code from the KB article along with my current code
below. Any help porting this for use on local PSTs is greatly appreciated!

http://support.microsoft.com/?kbid=194623
HOWTO: Use CDO (1.x) to See if a Message Is Signed and/or Encrypted
'Requires a reference to the Microsoft CDO (1.2 or 1.21) library

Dim strServer As String
Dim strMailbox As String
Dim strProfileInfo As String
Dim objSession As MAPI.Session
Dim objInbox As Folder
Dim objMessages As Messages
Dim objMessage As Message

'I am dimensioning my objects like this:
' Dim olFolder As Outlook.MAPIFolder
' Dim olmi As Outlook.MailItem
' Dim oli As Outlook.Items
' Dim olR As Outlook.Recipient
' Dim olrs As Outlook.Recipients

strServer = "MyExchangeServer" 'Insert name of an Exchange Server.
strMailbox = "MyMailbox" 'Insert the name of a Mailbox.

'Create your ProfileInfo string.
strProfileInfo = strServer & vbLf & strMailbox

'Create your session and log onto it on the fly.
Set objSession = New MAPI.Session
objSession.Logon "", "", False, True, 0, True, strProfileInfo

'I assume I do not need to create a session object for use with PSTs??

'Create your Inbox object and get all the messages in the inbox.
Set objInbox = objSession.Inbox
Set objMessages = objInbox.Messages

'Get the first message in the objMessages collection.
Set objMessage = objMessages.GetFirst

If objMessage Is Nothing Then
MsgBox "No messages to process"
Else
'Set up a loop to run through all the messages in the inbox.

Do
With objMessage
'Print the subject.
Debug.Print .Subject

'Print the Message Class.
Debug.Print .Fields(CdoPR_MESSAGE_CLASS).Value
Debug.Print
End With 'objMessage

'Get the next message.
Set objMessage = objMessages.GetNext
Loop Until objMessage Is Nothing
End If

'Here is my loop

'For Each olmi In olFolder.Items
' Set olrs = olmi.Recipients
' For Each olR In olrs 'check every recipient the message was
sent to
' If olR.Address = strE And olmi.SenderEmailAddress <>
olR.Address Then
' Call AddMatchSent(rsts, olmi, strE)
' End If
' Next
' 'check sender address
' If strFn <> "Sent Items" And olmi.SenderEmailAddress = strE
Then
' Call AddMatchRecd(rstr, olmi, strE)
' intRct = intRct + 1
' End If
' Next olmi
 
D

deko

Here's another crack at it... still needs some work

Dim objSession As MAPI.Session
Dim objInboxMsgs As MAPI.Messages
Dim objMsg As MAPI.Message
Dim strAddress As String
Dim strRecipient As String
Dim olns As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim objRecip As MAPI.Recipient
Const g_PR_SMTP_ADDRESS_W = &H39FE001F
Set objSession = CreateObject("MAPI.Session")
objSession.Logon NewSession:=False
Set objInboxMsgs = objSession.Inbox.Messages
For Each objMsg In objInboxMsgs
Debug.Print objMsg.Subject
strAddress = objMsg.Sender.Address
' If the e-mail sender is an Exchange user, the e-mail address
' will return as an X400 address and not an SMTP address.
' If the Address property does not return a fully qualified
' address (for example, containing an @), try the &H39FE001F MAPI
' IMPORTANT: This property is not documented on MSDN.
If Not InStr(strAddress, "@") Then
On Error Resume Next
strAddress = objMsg.Sender.Fields(g_PR_SMTP_ADDRESS_W).Value
strRecipient = objRecip.Address '<<=== this is not working
End If
Debug.Print strAddress
Debug.Print strRecipient
Next
objSession.Logoff
Set objMsg = Nothing
Set objInboxMsgs = Nothing
Set objSession = Nothing
 
K

Ken Slovak - [MVP - Outlook]

Each CDO Message object has a Recipients collection. You must set that in
order to then set a Recipient object for each member of the Recipients
collection. If you don't do that you cannot access any properties for any of
the Recipient's.

Be aware though that in secure versions of Outlook that access to CDO's
Recipients collection will fire the security prompts. Even in Outlook 2003,
where VBA code is inherently trusted if all Outlook objects are derived from
the Application object, CDO code is never trusted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top