Log in

View Full Version : Help accessing two diff outlook folders in VBA



swisscheez
01-15-2008, 12:27 PM
Hello group - I've gotten stuck on an issue and I'm reaching out for help -

I have a main inbox (a) that has it's own pst file.
I have a 2nd mail file where I store archives (b), in a diff pst file.

I'm trying to write a vba macro that will read messages from A do some processing and them move them to B if they meet certain criteria.

I can do the first part and I know how to move messages within a particular file but when I try to reference a folder in B, I get a message telling me that outlook cannot find it.

I'm quite sure that there is a way to do this - and I'm probably just not declaring things correctly - does anyone have any examples that I could review where two different Outlook folders are referenced and controlled using vba?

saban
02-11-2008, 01:51 PM
I am having exact same issue with tasks folder did yiu figure it out ???

It is really frustrating :((

swisscheez
07-31-2008, 03:41 PM
No I never found an answer - the issue is coming up again too. Did you find anything?

Mavyak
07-31-2008, 06:24 PM
I created a new pst file and captioned the main folder "Secondary Folder". I then created a folder in that folder called "Folder In Secondary Folder pst". I then wrote the code below and it moved a message from the inbox of one pst file to a folder in another pst file.

Sub TransferMessageBetweenPSTFiles()
Dim M As MailItem
Dim P As MAPIFolder
Dim S As MAPIFolder
Dim N As NameSpace
Dim obj As Object

Set N = Application.GetNamespace("MAPI")

Set P = N.GetDefaultFolder(olFolderInbox)

For Each obj In P.Items
If obj.Class = olMail Then
Set M = obj
Exit For
End If
Next obj
If M Is Nothing Then
MsgBox "You don't have any mail!"
GoTo ExitSub
End If
Set S = N.Folders("Secondary Folder").Folders("Folder In Secondary Folder pst")

M.Move S

ExitSub:
If Not obj Is Nothing Then
Set obj = Nothing
End If
If Not M Is Nothing Then
Set M = Nothing
End If
Set P = Nothing
Set S = Nothing
Set N = Nothing
End Sub

saban
08-20-2008, 07:57 AM
mistake sorry