A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Outlook Automation: is there a better way?



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old June 5th 08, 11:33 PM posted to microsoft.public.outlook.program_vba
deko
external usenet poster
 
Posts: 15
Default Outlook Automation: is there a better way?

I just upgraded to Office 2007 and would like to refactor my automation code
to improve speed and perhaps add some new functionality.

My Access 2007 database has code that searches Outlook 2007 for email
messages. The database contains Contacts, each with one or more email
addresses. The current code loops through each message in the Outlook Inbox
trying to match one of the Contact's email addresses with the
SenderEmailAddress property of each mail item. When a match is found, the
message subject and other data is added to a recordset and displayed in a
datasheet in Access.

Here's an abbreviated example:

Dim olapp As Outlook.Application
Dim olmi as Outlook.MailItem
Dim olitms as Outlook.Items
Dim objItem As Object

Set olapp = New Outlook.Application
Set olns = olapp.GetNamespace("MAPI")
Set olFolder = olns.GetDefaultFolder(olFolderInbox)
Set olitms = olFolder.Items

Do While Not rstContactAddresses.EOF
For Each objItem in olitms
If TypeOf objItem Is Outlook.MailItem Then
Set olmi = objItem
If olmi.SenderAddress = rstContactAddresses!EmailAddress Then
Call AddToRecordset(olmi.whatever)
End if
End if
Next objItem
Loop

Is there a better way to do this with VBA? Is it worth trying to use C# and
VSTO here? Is it possible to tap into the indexed Outlook email search
feature? Other new stuff in 2007 that can help?

Thanks in advance.

Ads
  #2  
Old June 6th 08, 01:19 AM posted to microsoft.public.outlook.program_vba
Dmitry Streblechenko
external usenet poster
 
Posts: 2,116
Default Outlook Automation: is there a better way?

Do not loop through all items in the folder, use Items.Find / FindNext.
Off teh top of my head:

set olmi = olitms.Find("[SenderEmailAddress] = '" &
rstContactAddresses!EmailAddress & "'")
while Not (olmi Is Nothing)
Call AddToRecordset(olmi.whatever)
set olmi = olitms.FindNext
wend
--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
"deko" wrote in message
...
I just upgraded to Office 2007 and would like to refactor my automation
code to improve speed and perhaps add some new functionality.

My Access 2007 database has code that searches Outlook 2007 for email
messages. The database contains Contacts, each with one or more email
addresses. The current code loops through each message in the Outlook
Inbox trying to match one of the Contact's email addresses with the
SenderEmailAddress property of each mail item. When a match is found, the
message subject and other data is added to a recordset and displayed in a
datasheet in Access.

Here's an abbreviated example:

Dim olapp As Outlook.Application
Dim olmi as Outlook.MailItem
Dim olitms as Outlook.Items
Dim objItem As Object

Set olapp = New Outlook.Application
Set olns = olapp.GetNamespace("MAPI")
Set olFolder = olns.GetDefaultFolder(olFolderInbox)
Set olitms = olFolder.Items

Do While Not rstContactAddresses.EOF
For Each objItem in olitms
If TypeOf objItem Is Outlook.MailItem Then
Set olmi = objItem
If olmi.SenderAddress = rstContactAddresses!EmailAddress Then
Call AddToRecordset(olmi.whatever)
End if
End if
Next objItem
Loop

Is there a better way to do this with VBA? Is it worth trying to use C#
and VSTO here? Is it possible to tap into the indexed Outlook email
search feature? Other new stuff in 2007 that can help?

Thanks in advance.



  #3  
Old June 6th 08, 03:43 AM posted to microsoft.public.outlook.program_vba
JP[_3_]
external usenet poster
 
Posts: 201
Default Outlook Automation: is there a better way?

In addition to what Dmitry suggested, you could also use the Restrict
Method to limit you search to ONLY those items in the Inbox that match
your criteria. This would significantly reduce the amount of time
spent looping through a folder.

See
http://msdn.microsoft.com/en-us/library/bb220369.aspx


--JP

On Jun 5, 6:33 pm, "deko" wrote:
I just upgraded to Office 2007 and would like to refactor my automation code
to improve speed and perhaps add some new functionality.

My Access 2007 database has code that searches Outlook 2007 for email
messages. The database contains Contacts, each with one or more email
addresses. The current code loops through each message in the Outlook Inbox
trying to match one of the Contact's email addresses with the
SenderEmailAddress property of each mail item. When a match is found, the
message subject and other data is added to a recordset and displayed in a
datasheet in Access.

Here's an abbreviated example:

Dim olapp As Outlook.Application
Dim olmi as Outlook.MailItem
Dim olitms as Outlook.Items
Dim objItem As Object

Set olapp = New Outlook.Application
Set olns = olapp.GetNamespace("MAPI")
Set olFolder = olns.GetDefaultFolder(olFolderInbox)
Set olitms = olFolder.Items

Do While Not rstContactAddresses.EOF
For Each objItem in olitms
If TypeOf objItem Is Outlook.MailItem Then
Set olmi = objItem
If olmi.SenderAddress = rstContactAddresses!EmailAddress Then
Call AddToRecordset(olmi.whatever)
End if
End if
Next objItem
Loop

Is there a better way to do this with VBA? Is it worth trying to use C# and
VSTO here? Is it possible to tap into the indexed Outlook email search
feature? Other new stuff in 2007 that can help?

Thanks in advance.


  #4  
Old June 6th 08, 06:09 AM posted to microsoft.public.outlook.program_vba
Dmitry Streblechenko
external usenet poster
 
Posts: 2,116
Default Outlook Automation: is there a better way?

Don't use Restrict unless the restriction stays the same: Exchange caches
restrictions for a week by default. Cached restriction arae updated every
time an item in the folder iss modified/added/deleted.
Unless you really want the restriction to be cached, do not use Restrict
against an Exchange store.

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
"JP" wrote in message
...
In addition to what Dmitry suggested, you could also use the Restrict
Method to limit you search to ONLY those items in the Inbox that match
your criteria. This would significantly reduce the amount of time
spent looping through a folder.

See
http://msdn.microsoft.com/en-us/library/bb220369.aspx


--JP

On Jun 5, 6:33 pm, "deko" wrote:
I just upgraded to Office 2007 and would like to refactor my automation
code
to improve speed and perhaps add some new functionality.

My Access 2007 database has code that searches Outlook 2007 for email
messages. The database contains Contacts, each with one or more email
addresses. The current code loops through each message in the Outlook
Inbox
trying to match one of the Contact's email addresses with the
SenderEmailAddress property of each mail item. When a match is found,
the
message subject and other data is added to a recordset and displayed in a
datasheet in Access.

Here's an abbreviated example:

Dim olapp As Outlook.Application
Dim olmi as Outlook.MailItem
Dim olitms as Outlook.Items
Dim objItem As Object

Set olapp = New Outlook.Application
Set olns = olapp.GetNamespace("MAPI")
Set olFolder = olns.GetDefaultFolder(olFolderInbox)
Set olitms = olFolder.Items

Do While Not rstContactAddresses.EOF
For Each objItem in olitms
If TypeOf objItem Is Outlook.MailItem Then
Set olmi = objItem
If olmi.SenderAddress = rstContactAddresses!EmailAddress Then
Call AddToRecordset(olmi.whatever)
End if
End if
Next objItem
Loop

Is there a better way to do this with VBA? Is it worth trying to use C#
and
VSTO here? Is it possible to tap into the indexed Outlook email search
feature? Other new stuff in 2007 that can help?

Thanks in advance.




  #5  
Old June 6th 08, 07:25 AM posted to microsoft.public.outlook.program_vba
deko
external usenet poster
 
Posts: 15
Default Outlook Automation: is there a better way?

Thanks for the heads up, Dmitry.

I'm wondering if there's a way to force the cache to clear itself. Also, I
assume on a local Outlook PST there are no cache issues.

From reading the MSDN article, the Restrict method seems to the better
option for my needs: "The Restrict method is significantly faster if there
is a large number of items in the collection, especially if only a few items
in a large collection are expected to be found." This is the case when
searching a large Inbox for messages matching one or two email addresses.

What's really interesting is the ability to use an SQL query as the filter:

The example in the MSDN article is this:
filter = /mapi/proptag/0x0037001f"" =
'the right ""stuff""'"

What is that hyperlink? Is that just part of the example in the MSDN
article?

How about: "@SQL = "SELECT EmailAddress from tblContactEmailAddresses" -
would this work as a Restrict filter?


"Dmitry Streblechenko" wrote in message
...
Don't use Restrict unless the restriction stays the same: Exchange caches
restrictions for a week by default. Cached restriction arae updated every
time an item in the folder iss modified/added/deleted.
Unless you really want the restriction to be cached, do not use Restrict
against an Exchange store.

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
"JP" wrote in message
...
In addition to what Dmitry suggested, you could also use the Restrict
Method to limit you search to ONLY those items in the Inbox that match
your criteria. This would significantly reduce the amount of time
spent looping through a folder.

See
http://msdn.microsoft.com/en-us/library/bb220369.aspx


--JP

On Jun 5, 6:33 pm, "deko" wrote:
I just upgraded to Office 2007 and would like to refactor my automation
code
to improve speed and perhaps add some new functionality.

My Access 2007 database has code that searches Outlook 2007 for email
messages. The database contains Contacts, each with one or more email
addresses. The current code loops through each message in the Outlook
Inbox
trying to match one of the Contact's email addresses with the
SenderEmailAddress property of each mail item. When a match is found,
the
message subject and other data is added to a recordset and displayed in
a
datasheet in Access.

Here's an abbreviated example:

Dim olapp As Outlook.Application
Dim olmi as Outlook.MailItem
Dim olitms as Outlook.Items
Dim objItem As Object

Set olapp = New Outlook.Application
Set olns = olapp.GetNamespace("MAPI")
Set olFolder = olns.GetDefaultFolder(olFolderInbox)
Set olitms = olFolder.Items

Do While Not rstContactAddresses.EOF
For Each objItem in olitms
If TypeOf objItem Is Outlook.MailItem Then
Set olmi = objItem
If olmi.SenderAddress = rstContactAddresses!EmailAddress
Then
Call AddToRecordset(olmi.whatever)
End if
End if
Next objItem
Loop

Is there a better way to do this with VBA? Is it worth trying to use C#
and
VSTO here? Is it possible to tap into the indexed Outlook email search
feature? Other new stuff in 2007 that can help?

Thanks in advance.





  #6  
Old June 6th 08, 10:48 AM posted to microsoft.public.outlook.program_vba
deko
external usenet poster
 
Posts: 15
Default Outlook Automation: is there a better way?

To answer my own question:

Those links are schema property names used in the SQL-like DASL syntax,
which begins with @SQL=

See: http://blogs.msdn.com/rgregg/archive...12/502904.aspx

If you put the below code in a module and run it, you get this:

? FindMessages()
@SQL =("urn:schemas:httpmail:Subject" CI_PHRASEMATCH 'test')
Error Number -2147352567: Cannot parse condition. Error at "CI_PHRASEMATCH".

How is my syntax incorrect? Am I missing something?

Public Function FindMessages()
On Error GoTo HandleErr
Const QT As String = """"
Dim olapp As Outlook.Application
Dim olns As Outlook.NameSpace
Dim olfldr As Outlook.Folder
Dim olmi As Outlook.MailItem
Dim olitms As Outlook.Items
Dim objItem As Object
Dim strFilter As String

Set olapp = New Outlook.Application
Set olns = olapp.GetNamespace("MAPI")
Set olfldr = olns.GetDefaultFolder(olFolderInbox)
Set olitms = olfldr.Items

'===== available fields ====='
'attachmentfilename
'httpmail -BCC
'httpmail -CC
'content-disposition-type
'content-media-type
'httpmail -Date
'datereceived
'httpmail -From
'HasAttachment
'htmldescription
'Importance
'normalizedsubject
'Priority
'httpmail-reply-to
'Sender
'Subject
'textdescription
'thread -topic
'to

'===== Keywords ====='
'CI_PHRASEMATCH
'CI_PHRASEMATCH

strFilter = "@SQL =(" & QT & "urn:schemas:httpmail:Subject" & QT & "
CI_PHRASEMATCH 'test')"
Debug.Print strFilter
objItems = olitms.Restrict(strFilter)
Debug.Print objItems.Count
Exit_He
On Error Resume Next
Set olitms = Nothing
Set olfldr = Nothing
Set olns = Nothing
Set olapp = Nothing
Exit Function
HandleErr:
Debug.Print "Error Number " & Err.Number & ": " & Err.Description
Resume Exit_He
End Function

  #7  
Old June 6th 08, 12:25 PM posted to microsoft.public.outlook.program_vba
deko
external usenet poster
 
Posts: 15
Default Outlook Automation: is there a better way?

Beta 2

does not error out, but the filter does not seem to work
note: messages exist in the Inbox that wd match filter criteria

? FindMessages()
562 unfiltered
@SQL="urn:schemas:httpmail:Subject" CI_PHRASEMATCH 'Flight'
0 filtered

Public Function FindMessages()
Const QT As String = """"
On Error GoTo HandleErr
Dim olapp As Outlook.Application
Dim olns As Outlook.NameSpace
Dim olfldr As Outlook.Folder
Dim olmi As Outlook.MailItem
Dim olitms As Outlook.Items
Dim matchedItems As Outlook.Items
Dim strFilter As String

Set olapp = New Outlook.Application
Set olns = olapp.GetNamespace("MAPI")
Set olfldr = olns.GetDefaultFolder(olFolderInbox)
Set olitms = olfldr.Items

'===== available fields ====='
'attachmentfilename
'httpmail -BCC
'httpmail -CC
'content-disposition-type
'content-media-type
'httpmail -Date
'datereceived
'httpmail -From
'HasAttachment
'htmldescription
'Importance
'normalizedsubject
'Priority
'httpmail-reply-to
'Sender
'Subject
'textdescription
'thread -topic
'to

'===== Keywords ====='
'CI_STARTSWITH
'CI_PHRASEMATCH

'strFilter = "@SQL=" & QT & "urn:schemas:httpmail:HasAttachment" & QT &
" = 1"
strFilter = "@SQL=" & QT & "urn:schemas:httpmail:Subject" & QT & "
CI_PHRASEMATCH 'Flight'"
Debug.Print olitms.Count & " unfiltered"
Debug.Print strFilter
Set matchedItems = olitms.Restrict(strFilter)
Debug.Print matchedItems.Count & " filtered"
Exit_He
On Error Resume Next
Set matchedItems = Nothing
Set olitms = Nothing
Set olfldr = Nothing
Set olns = Nothing
Set olapp = Nothing
Exit Function
HandleErr:
Debug.Print "Error Number " & Err.Number & ": " & Err.Description
Resume Exit_He
End Function

  #8  
Old June 6th 08, 01:33 PM posted to microsoft.public.outlook.program_vba
deko
external usenet poster
 
Posts: 15
Default Outlook Automation: is there a better way?

the missing link...

http://msdn.microsoft.com/en-us/libr...EXCHG.65).aspx

urn:schemas:mailheader:approved
urn:schemas:httpmail:attachmentfilename
urn:schemas:mailheader:bcc
urn:schemas:httpmail:bcc
urn:schemas:httpmail:cc
urn:schemas:mailheader:cc
urn:schemas:mailheader:comment
urn:schemas:mailheader:content-base
urn:schemas:mailheader:content-class
urn:schemas:mailheader:content-description
urn:schemas:mailheader:content-disposition
urn:schemas:httpmail:content-disposition-type
urn:schemas:mailheader:content-id
urn:schemas:mailheader:content-language
urn:schemas:mailheader:content-location
urn:schemas:httpmail:content-media-type
urn:schemas:mailheader:content-transfer-encoding
urn:schemas:mailheader:content-type
urn:schemas:mailheader:control
urn:schemas:httpmail:date
urn:schemas:mailheader:date
urn:schemas:httpmail:datereceived
urn:schemas:httpmail:displaycc
urn:schemas:httpmail:displayto
urn:schemas:mailheader:disposition
urn:schemas:mailheader:disposition-notification-to
urn:schemas:mailheader:distribution
urn:schemas:mailheader:expires
urn:schemas:mailheader:expiry-date
urn:schemas:httpmail:flagcompleted
urn:schemas:mailheader:followup-to
urn:schemas:httpmail:from
urn:schemas:mailheader:from
urn:schemas:httpmail:fromemail
urn:schemas:httpmail:fromname
urn:schemas:httpmail:hasattachment
urn:schemas:httpmail:htmldescription
urn:schemas:httpmail:importance
urn:schemas:mailheader:importance
urn:schemas:mailheader:in-reply-to
urn:schemas:mailheader:keywords
urn:schemas:mailheader:lines
urn:schemas:mailheader:message-id
urn:schemas:httpmail:messageflag
urn:schemas:mailheader:mime-version
urn:schemas:mailheader:newsgroups
urn:schemas:httpmail:normalizedsubject
urn:schemas:mailheaderrganization
urn:schemas:mailheaderriginal-recipient
urn:schemas:mailheaderath
urn:schemas:mailheaderosting-version
urn:schemas:httpmailriority
urn:schemas:mailheaderriority
urn:schemas:mailheader:received
urn:schemas:mailheader:references
urn:schemas:mailheader:relay-version
urn:schemas:httpmail:reply-by
urn:schemas:mailheader:reply-by
urn:schemas:httpmail:reply-to
urn:schemas:mailheader:reply-to
urn:schemas:mailheader:return-path
urn:schemas:mailheader:return-receipt-to
urn:schemas:httpmail:savedestination
urn:schemas:httpmail:saveinsent
urn:schemas:mailheader:sender
urn:schemas:httpmail:sender
urn:schemas:httpmail:senderemail
urn:schemas:httpmail:sendername
http://schemas.microsoft.com/exchange/sensitivity
urn:schemas:mailheader:sensitivity
urn:schemas:httpmail:subject
urn:schemas:mailheader:subject
urn:schemas:httpmail:submitted
urn:schemas:mailheader:summary
urn:schemas:httpmail:textdescription
urn:schemas:mailheader:thread-index
urn:schemas:mailheader:thread-topic
urn:schemas:httpmail:thread-topic
urn:schemas:httpmail:to
urn:schemas:mailheader:to
urn:schemas:mailheader:x-mailer
urn:schemas:mailheader:x-message-completed
urn:schemas:mailheader:x-message-flag
urn:schemas:mailheader:x-unsent
urn:schemas:mailheader:xref
  #9  
Old June 6th 08, 04:28 PM posted to microsoft.public.outlook.program_vba
deko
external usenet poster
 
Posts: 15
Default Outlook Automation: is there a better way?

This works nicely, too:

Dim olapp As Outlook.Application
Dim olfldr As Outlook.Folder
Dim exp As Outlook.Explorer
Dim strSearch As String

Set olapp = New Outlook.Application
Set olfldr = olns.GetDefaultFolder(olFolderInbox)

strSearch = "body:beta _
subject:msdn _
_
received:=5/1/2008 12:00 AM 6/1/2008 12:00 AM"

Set exp = olapp.Explorers.Add(olfldr, olFolderDisplayNoNavigation)
exp.Search strSearch, olSearchScopeCurrentFolder
exp.Display

With additional code I can dynamically select a folder as well as pass in
variables for those 4 search criteria.


  #10  
Old June 6th 08, 06:17 PM posted to microsoft.public.outlook.program_vba
Dmitry Streblechenko
external usenet poster
 
Posts: 2,116
Default Outlook Automation: is there a better way?

Correct, PST is not subject to this limitation.
There is no way to clear the cache.

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
"deko" wrote in message
...
Thanks for the heads up, Dmitry.

I'm wondering if there's a way to force the cache to clear itself. Also,
I assume on a local Outlook PST there are no cache issues.

From reading the MSDN article, the Restrict method seems to the better
option for my needs: "The Restrict method is significantly faster if there
is a large number of items in the collection, especially if only a few
items in a large collection are expected to be found." This is the case
when searching a large Inbox for messages matching one or two email
addresses.

What's really interesting is the ability to use an SQL query as the
filter:

The example in the MSDN article is this:
filter = /mapi/proptag/0x0037001f"" =
'the right ""stuff""'"

What is that hyperlink? Is that just part of the example in the MSDN
article?

How about: "@SQL = "SELECT EmailAddress from tblContactEmailAddresses" -
would this work as a Restrict filter?


"Dmitry Streblechenko" wrote in message
...
Don't use Restrict unless the restriction stays the same: Exchange caches
restrictions for a week by default. Cached restriction arae updated every
time an item in the folder iss modified/added/deleted.
Unless you really want the restriction to be cached, do not use Restrict
against an Exchange store.

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
"JP" wrote in message
...
In addition to what Dmitry suggested, you could also use the Restrict
Method to limit you search to ONLY those items in the Inbox that match
your criteria. This would significantly reduce the amount of time
spent looping through a folder.

See
http://msdn.microsoft.com/en-us/library/bb220369.aspx


--JP

On Jun 5, 6:33 pm, "deko" wrote:
I just upgraded to Office 2007 and would like to refactor my automation
code
to improve speed and perhaps add some new functionality.

My Access 2007 database has code that searches Outlook 2007 for email
messages. The database contains Contacts, each with one or more email
addresses. The current code loops through each message in the Outlook
Inbox
trying to match one of the Contact's email addresses with the
SenderEmailAddress property of each mail item. When a match is found,
the
message subject and other data is added to a recordset and displayed in
a
datasheet in Access.

Here's an abbreviated example:

Dim olapp As Outlook.Application
Dim olmi as Outlook.MailItem
Dim olitms as Outlook.Items
Dim objItem As Object

Set olapp = New Outlook.Application
Set olns = olapp.GetNamespace("MAPI")
Set olFolder = olns.GetDefaultFolder(olFolderInbox)
Set olitms = olFolder.Items

Do While Not rstContactAddresses.EOF
For Each objItem in olitms
If TypeOf objItem Is Outlook.MailItem Then
Set olmi = objItem
If olmi.SenderAddress = rstContactAddresses!EmailAddress
Then
Call AddToRecordset(olmi.whatever)
End if
End if
Next objItem
Loop

Is there a better way to do this with VBA? Is it worth trying to use
C# and
VSTO here? Is it possible to tap into the indexed Outlook email search
feature? Other new stuff in 2007 that can help?

Thanks in advance.






 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook Automation Automation Problem Out of Memory MikeA[_2_] Outlook and VBA 7 July 30th 07 02:31 PM
Problem with Outlook Automation Neil Outlook and VBA 13 January 15th 07 07:09 AM
Outlook Automation Tylendal Outlook and VBA 3 April 24th 06 02:39 PM
Outlook automation using vbs Marceepoo Outlook - Installation 1 January 17th 06 04:18 AM
Help on Outlook automation Neo Outlook - General Queries 1 January 15th 06 04:49 PM


All times are GMT +1. The time now is 02:26 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.