PDA

View Full Version : Outlook VBA



Nikitas10
09-29-2010, 08:13 AM
Hello,
I'm trying to use a 'Items.Restrict' command to isolate mail items to create a report. I'm trying to run that command on a selected mail of a random mail folder, not the default inbox. Please, can you help?
My code is

Dim logit As MailItem
Dim gUserName, sconv, sfrom, ssub, sFolder As String
Dim icounter As Integer
Dim itms As Outlook.Items
Dim itm As Outlook.MailItem
Dim fld As Outlook.MAPIFolder

If Application.ActiveExplorer.Selection.Count > 1 Then
MsgBox ("Only a single email can be logged at any time.")
Exit Sub
End If

Set logit = Application.ActiveExplorer.Selection.Item(1)
If logit.Class <> olMail Then
MsgBox ("Only mail items can be logged.")
Exit Sub
End If

gUserName = TheCurrentUser()

sFolder = logit.Parent.Name
sconv = logit.ConversationIndex

sfrom = logit.SenderName
ssub = logit.Subject


Set fld = Application.Session.GetDefaultFolder(olFolderInbox)

'This targets my Inbox, I have to find how to use the folder called sFolder from the selected item !!!!! I need to somehow define the fld correctly

strFind = "[ConversationTopic]=" & Chr(34) & logit.ConversationTopic & Chr(34)

Set itms = fld.Items.Restrict(strFind)

icounter = 0
For Each itm In itms

'I'll write the code here.

icounter = icounter + 1
Next

Any help is much appreciated. Thanks

Charlize
10-13-2010, 06:52 AM
Change Set logit = Application.ActiveExplorer.Selection.Item(1)toSet logit = Application.ActiveExplorer.Selection(1) and application isn't needed I think, because you are in outlook.

Charlize

Nikitas10
10-14-2010, 07:00 AM
Hello,
thank you very much for that. Sorry for taking this long to reply.
What you suggested does make a difference but still the
Set itms = fld.Items.Restrict (strFind)
returns blank.
How should I change the
Set fld = Application.Session.GetDefaultFolder(oFolderInbox) ?
Thank you for the help

Charlize
10-14-2010, 07:52 AM
Little example. Paste this in a module, select a message in some folder and perform this when using F8 in the vba editor.
Sub Show_folder_of_message()
'the mailitem
Dim mymessage As Outlook.MailItem
'the folder of the mailitem
Dim myfolder As Outlook.Folder
'put active mailmessage in your variable
Set mymessage = ActiveExplorer.Selection.Item(1)
'display the folder of the message
MsgBox mymessage.Parent
'put the folder of active message in variable
Set myfolder = mymessage.Parent
'display number of items in the folder of the mailmessage you selected
MsgBox myfolder.Items.Count
End SubCharlize

Charlize
10-15-2010, 12:32 AM
Set fld = logit.parent will give the folder of the active item (I think).

Charlize

Nikitas10
10-20-2010, 02:35 AM
Thanks a lot for that. That is really what I needed.
Cheers :clap: