Microsoft Outlook Macro - Selecting a different mailbox

bhavin_85

New member
Local time
Today, 03:26
Joined
Dec 20, 2007
Messages
1
**I posted the same topic in the windows section but figured it would make more sense in this section of the forum**

hey guys

Ive had a quick search around and couldnt find anything that was remotely close to what I need.

Ive written the following code to run as a macro to send select emails into a folder when the button is clicked on. However because this isnt my default mailbox in my outlook I keep getting the error message I built in. How do I get outlook to look at a specific name mailbox instead of the default one? I know I ahve to change the objInbox = objNS.GetDefaultFolder(olFolderInbox) but to what?
Code:
Sub ActionedNFA()
On Error Resume Next
 
	Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
	Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem
 
	Set objNS = Application.GetNamespace("MAPI")
	Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
	Set objFolder = objInbox.Folders("1 Action Taken - no further action")
'Assume this is a mail folder
 
	If objFolder Is Nothing Then
		MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
	End If
 
	If Application.ActiveExplorer.Selection.Count = 0 Then
		'Require that this procedure be called only when a message is selected
		Exit Sub
	End If
 
	For Each objItem In Application.ActiveExplorer.Selection
		If objFolder.DefaultItemType = olMailItem Then
			If objItem.Class = olMail Then
				objItem.Move objFolder
			End If
		End If
	Next
 
	Set objItem = Nothing
	Set objFolder = Nothing
	Set objInbox = Nothing
	Set objNS = Nothing
 
End Sub

Tahnks for the help guys :)
 
You are probably looking for something like this :

Filefolder = "Some Folder"
Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
Set objMailbox = objInbox.Parent 'This object is the whole mailbox
Set objFileFolder = objMailbox.Folders(Filefolder)

The trick is to use the parent and use the Folders method to find the folder you need. It works on all folders (even special folders like Inbox, Deleted Items and Sent Items which have constants like olFolderInbox that can also be used)

-M



**I posted the same topic in the windows section but figured it would make more sense in this section of the forum**

hey guys

Ive had a quick search around and couldnt find anything that was remotely close to what I need.

Ive written the following code to run as a macro to send select emails into a folder when the button is clicked on. However because this isnt my default mailbox in my outlook I keep getting the error message I built in. How do I get outlook to look at a specific name mailbox instead of the default one? I know I ahve to change the objInbox = objNS.GetDefaultFolder(olFolderInbox) but to what?
Code:
Sub ActionedNFA()
On Error Resume Next
 
    Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
    Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem
 
    Set objNS = Application.GetNamespace("MAPI")
    Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
    Set objFolder = objInbox.Folders("1 Action Taken - no further action")
'Assume this is a mail folder
 
    If objFolder Is Nothing Then
        MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
    End If
 
    If Application.ActiveExplorer.Selection.Count = 0 Then
        'Require that this procedure be called only when a message is selected
        Exit Sub
    End If
 
    For Each objItem In Application.ActiveExplorer.Selection
        If objFolder.DefaultItemType = olMailItem Then
            If objItem.Class = olMail Then
                objItem.Move objFolder
            End If
        End If
    Next
 
    Set objItem = Nothing
    Set objFolder = Nothing
    Set objInbox = Nothing
    Set objNS = Nothing
 
End Sub

Tahnks for the help guys :)
 

Users who are viewing this thread

Back
Top Bottom