I tested this on my Inbox, but my boss wants me to run it on a
subfolder located in his "Search Folders". Apparently Outlook does
not recognize "Search Folders" as a mailbox folder.
So my question is, how can I access this folder?
Dim iMailItem As Integer
Dim dteStart As String
Dim dteEnd As String
Dim myOutlook As Outlook.Application
Dim myNameSpace As Namespace
Dim mItemCollection As Items
Dim myMailItem As MailItem
Dim SafeMail
Dim sUser As String
Dim sMailbox As String
Dim sFolder As String
Dim sSaveLocation As String
On Error GoTo ErrorHandler
'U S E R V A R I A B L E S
sUser = "Fname"
sMailbox = "Mailbox - LName, FName"
sFolder = "outlook:Search Folders\Current Sent & Received\"
dteStart = (Date - 1 & " 00:00")
dteEnd = (Date - 1 & " 23:59")
Set myOutlook = CreateObject("Outlook.Application")
Set myNameSpace = myOutlook.GetNamespace("MAPI")
Set mItemCollection = myNameSpace.Folders(sMailbox).Folders("Search
Folders").Folders("Current Sent & Received") 'FAILS ON THIS LINE
'Set Filter to previous day
Set mItemCollection = mItemCollection.Restrict("[SentOn] >= '" &
dteStart & "'")
Set mItemCollection = mItemCollection.Restrict("[SentOn] <= '" &
dteEnd & "'")
mItemCollection.Sort "[SentOn]", False
ETC
In Outlook 2007 you'd use NameSpace.DefaultStore.GetSearchFolders() to get
the Folders collection of search folders. From there you can get the one of
interest.
In Outlook 2003 or earlier you'd need to be using a different API such as
Redemption (www.dimastr.com).
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
"McKilty" <blues...@gmail.com> wrote in message
news:d45ba78d-665c-4353...@c28g2000yqd.googlegroups.com...
Thanks Ken.
Unfortunately, Outlook 2003. We do have and use Redemption, so I'm
hoping I can find an example on his site because I'm not sure I can
figure put how to write this.
--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
"McKilty" <blues...@gmail.com> wrote in message
news:7a425929-5cec-4f31...@d10g2000yqh.googlegroups.com...
Thank you very much Dmitry.
I'm afraid I need more help. So, again, what we want to do is all all
the Sent & Received items for the previous day. Then we record the
From, To, CC, Subject, and first 100 characters in a spreadsheet.
Using Dmitry's code, I was able to find the folder, but I don't know
how to access the items in it.
1 - Can I set a Collections to the search folder so I can then filter
as normal?
I realize that I could also create a search folder on the fly for the
previous day rather than filter, but still, I need to know how to
access the e-mails and pull the info.
Here is my code so far:
-------------------------------------------------------------------------------------------
Dim iMailItem As Integer
Dim dteStart As String
Dim dteEnd As String
Dim myOutlook As Outlook.Application
Dim myNameSpace As Namespace
Dim mItemCollection As Items
Dim myMailItem As MailItem
'Dim myEmail As Outlook.MailItem
Dim SafeMail
Dim sUser As String
Dim sMailbox As String
Dim sFolder As String
Dim sSaveLocation As String
Dim Session
Dim Search
Dim Searches
Dim SRFolder
'On Error GoTo ErrorHandler
'U S E R V A R I A B L E S
''Rick Bray
******************************************************************
sUser = "rbray"
sMailbox = "Mailbox - Bray, Rick"
dteStart = (Date - 1 & " 00:00")
dteEnd = (Date - 1 & " 23:59")
Set myOutlook = CreateObject("Outlook.Application")
Set myNameSpace = myOutlook.GetNamespace("MAPI")
Set Session = CreateObject("Redemption.RDOSession")
Session.Logon
Set Searches = Session.Stores.DefaultStore.Searches
For i = 1 To Searches.Count
Set Search = Searches.Item(i)
If Search.Name = "Current Sent & Received" Then
Exit For
End If
Next
'I don't know what this line does or what I can/should do with it.
SRFolder = Search.GetFolder(True)
' This next section is from my existing code.
' I'd like to set mItemCollection to SRFolder, or the contents of
Search, but I can't figure that out.
Set mItemCollection = myNameSpace.Folders(sMailbox).Folders("Search
Folders").Folders("Current Sent & Received")
'Set Filter to previous day
Set mItemCollection = mItemCollection.Restrict("[SentOn] >= '" &
dteStart & "'")
Set mItemCollection = mItemCollection.Restrict("[SentOn] <= '" &
dteEnd & "'")
mItemCollection.Sort "[SentOn]", False
For iMailItem = 1 To mItemCollection.Count
Set myMailItem = mItemCollection.Item(iMailItem)
Set SafeMail = CreateObject("Redemption.SafeMailItem")
SafeMail.Item = myMailItem
sSender = SafeMail.SenderName
sSentTo = SafeMail.To
etc.
-------------------------------------------------------------------------------------------
Thanks for your help.
If you create a search folder yourself in code you can get the MAPIFolder
reference passed by the Save() function and get the folder EntryID and use
that later to get the folder using NameSpace.GetFolderFromID(). After that
you work with the Items collection.
I'm not sure what your code is trying to do other than using non-existent
properties from Redemption. There is no RDOStore.Searches property or
collection to work with, I'm not sure where you came up with that.
Code like this will get you to the search root folder, from there you
iterate the folders collection of the search root:
Dim SearchRoot As Redemption.RDOFolder
Set SearchRoot = Session.Stores.DefaultStore.SearchRootFolder
Set Search = SearchRoot.Folders.Item("Current Sent & Received")
Set searchItems = Search.Items ' RDOItems
Then just iterate the items collection.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
"McKilty" <blues...@gmail.com> wrote in message
news:386b85b7-5560-4736...@g31g2000yqc.googlegroups.com...
On Sep 29, 5:01 pm, McKilty <bluesbr...@gmail.com> wrote:
<snip>
Thanks Ken. I'll give that a shot. As for where I got the code, I
copied it from Dmitry's site. All I did was alter it to stop when it
found the folder I wanted.
--------------
My Code
--------------
Set Session = CreateObject("Redemption.RDOSession")
Session.Logon
Set Searches = Session.Stores.DefaultStore.Searches
For i = 1 To Searches.Count
Set Search = Searches.Item(i)
If Search.Name = "Current Sent & Received" Then
Exit For
End If
Next
---------------------
Dmitry's Code
---------------------
set Session = CreateObject("Redemption.RDOSession")
Session.Logon
set Searches = Session.Stores.DefaultStore.Searches
for i = 1 to Searches.Count
set Search = Searches.Item(i)
Debug.Print Search.Name & ": " & Search.SearchCriteria.AsSQL
next
The line that reads "SRFolder = Search.GetFolder(True) " is one that I
wrote as I tried to experiment. I don't know what it does though, so
I'll remove that. ;)
Anyway, thanks again.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
"McKilty" <blues...@gmail.com> wrote in message
news:29c45f23-639d-41b8...@d10g2000yqh.googlegroups.com...
<snip>
So for VBA code you could use something like this, assuming you're using a
PST file as your default store:
Dim oStore As Redemption.RDOPstStore
Set Session = CreateObject("Redemption.RDOSession")
Session.Logon
Set oStore = Session.Stores.DefaultStore
Set Searches = oStore.Searches
Set Search = Searches.Item("Current Sent & Received")
If you're using an Exchange mailbox substitute RDOExchangeMailboxStore in
the declaration of oStore.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
"Ken Slovak - [MVP - Outlook]" <kens...@mvps.org> wrote in message
news:O%236OyTeQ...@TK2MSFTNGP05.phx.gbl...
Thanks so much for your help. I'm completely self taught, so
obviously I struggle quite a bit. ;)
When I wrote this for a regular folder it worked, but for the Search
Folders, it does not work. My hyperlink is:
sHyperLink = "outlook:search folders\Current Sent & Received\" &
mItemCollection.Item(iMailItem).EntryID
It is getting the EntryID OK, but I'm thinking you cannot link to the
Search Folders. Is there a way to get the objects original location
and link back to it?
Using indexed access to an item in a search folder is error prone due to
item moving around in the collection as more or fewer items meet the filter
criteria. In fact persisting indexed access to any member of any Items
collection is error prone. Say you are linking to items in Inbox, how often
does that collection get changed?
Your hyperlinks should all be in the format "Outlook:entryID". That way as
long as the items still exist in the same folder you will reference them.
With PST files, where EntryID doesn't change if the item remains in the
store, you would be able to reference the correct items even if they were
moved to different folders. That wouldn't work with Exchange stores or if
items were moved from one store to another.
With a current collection index you can get the actual parent folder name
from the item this way, assuming folder.Items.Item(iMailItem).Parent.Name.
You can get the EntryID as a string the same way.
See http://www.slipstick.com/outlook/links.htm for information on formatting
various sorts of links.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
"McKilty" <blues...@gmail.com> wrote in message
news:9ba1a935-96b3-4bfa...@k19g2000yqc.googlegroups.com...
So, I said I got it working... well, of course that's on my
development computer. When I run it on the end-users computer I get
an error when I try to use a filter. The exact line of code it
crashes on is:
---------------------------------
Set mItemCollection = mItemCollection.Restrict("[SentOn] > '" &
dteStart & "' AND [SentOn] < '" & dteEnd & "'")
---------------------------------
The error is
Run-time error '438':
Object doesn't support this property or method
The Code that proceeds this is:
----------------------------------
Dim dteStart As String
Dim dteEnd As String
Dim myOutlook As Outlook.Application
Dim myNameSpace As Namespace
Dim myMailItem
Dim sUser As String
Dim sMailbox As String
Dim sSaveLocation As String
Dim Session
Dim Search
Dim SearchRoot As Redemption.RDOFolder
On Error GoTo ErrorHandler
'U S E R V A R I A B L E S
' Rick Bray
******************************************************************
sUser = "rbray"
sMailbox = "Mailbox - Bray, Rick"
dteStart = (Date - 1 & " 00:00")
dteEnd = (Date - 1 & " 23:59")
Set myOutlook = CreateObject("Outlook.Application")
Set myNameSpace = myOutlook.GetNamespace("MAPI")
Set Session = CreateObject("Redemption.RDOSession")
Session.Logon
Set SearchRoot = Session.Stores.DefaultStore.SearchRootFolder
Set Search = SearchRoot.Folders.Item("Current Sent & Received")
Set mItemCollection = Search.Items ' RDOItems
Set mItemCollection = mItemCollection.Restrict("[SentOn] > '" &
dteStart & "' AND [SentOn] < '" & dteEnd & "'")
'<-------------------------- FAIL
----------------------------------
I also tried:
'Set mItemCollection = mItemCollection.Restrict("[SentOn] > '" & Format
(dteStart, "yyyy-mm-dd") & "' AND [SentOn] < '" & Format(dteEnd, "yyyy-
mm-dd") & "'")
AND
'Set mItemCollection = mItemCollection.Restrict("[SentOn] > '" &
dteStart & "'")
'Set mItemCollection = mItemCollection.Restrict("[SentOn] < '" &
dteEnd & "'")
They all fail. I stumped on this one because like I said, it works on
my computer but not hers.
Thank you so much for your help.
Dim dteStart as string
to a date, and then back to a string and it worked. This makes no
sense, so I may be mistaken. Anyway, now it kinda works...
This line filters the e-mails to NOTHING.
Set mItemCollection = mItemCollection.Restrict("[SentOn] > '" & Format
(dteStart, "yyyy-mm-dd") & "' AND [SentOn] < '" & Format(dteEnd, "yyyy-
mm-dd") & "'")
These lines I don't work because the second line overwrites the first
line and I just get everything BEFORE the end date.
Set mItemCollection = mItemCollection.Restrict("[SentOn] > '" & Format
(dteStart, "yyyy-mm-dd") & "'")
Set mItemCollection = mItemCollection.Restrict("[SentOn] < '" & Format
Thanks for all your help Ken, Dmitry, and Sue (from another thread.)