Outlook VBA - moving mail item to public folder using variable within path

Status
Not open for further replies.

aaroncrt

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Hi,

First of all, I am new to VBA so hopefully this will be an easy fix.

I have created a VBA script to move a selected email to a public folder location. The script prompts using an inputbox (in this example it's a project folder "001") and moves the email to the public folder called >Projects>"001 - TEST FOLDER". It uses the inputbox data as the folder location.

It is currently working until the folders reach above 099. All Public Folders are labelled like "001 - Test Folder", "023 - Project Name", "042 - Random" etc.

Is it just a case that I'm using the wrong variable type for the inputbox data?

Any help is appreciated.

Code:
Sub MoveProject() 
 
'Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder 
 
Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem 
 
Dim strProject As String 
 
Dim Proceed As VbMsgBoxResult 
 
Set objNS = Application.GetNamespace("MAPI") 
 
Dim appOutlook As New Outlook.Application 
 
Set nms = appOutlook.GetNamespace("MAPI") 
 
strProject = InputBox("Please enter Project") 
 
' If the public folder location is not \\public folders\ all public folders\projects\001 etc then the below line is required to be changed 
 
' Set objFolder = objNS.Folders("Public Folders").Folders("All Public Folders").Folders("Projects").Folders(strProject) 
 
strFolder = nms.Session.GetDefaultFolder(olPublicFoldersAllPublicFolders).Parent 
 
Set fld = nms.Folders(strFolder).Folders("All Public Folders").Folders("Projects").Folders(strProject)
  
 
For intX = 1 To objNS.Folders.Count 
 
If objNS.Folders.Item(intX).Name = "Public Folders" Then 
 
Exit For 
 
End If 
 
Next 
 
If Application.ActiveExplorer.Selection.Count = 0 Then 
 
'Require that this procedure be called only when a message is Selected 
 
Exit Sub 
 
End If 
 
Set oSelection = Application.ActiveExplorer.Selection 
 
For intX = ActiveExplorer.Selection.Count To 1 Step -1 
 
Set objX = ActiveExplorer.Selection.Item(intX) 
 
If objX.Class = olMail Then
   Proceed = MsgBox("Are you sure you want move the message to the Projects Folder " & strProject & "?", _
   vbYesNo + vbQuestion, "Confirm Move")
   If Proceed = vbYes Then 
 
Set objEmail = objX 
 
objEmail.Move fld
   End If 
 
End If 
 
Next 
 
Set objItem = Nothing 
 
Set objFolder = Nothing 
 
Set objInbox = Nothing 
 
Set objNS = Nothing 
 
End Sub
 

aaroncrt

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
it gives me

"Run-time error '440':

Array index out of bounds."

Debug then highlights "Set fld = nms.Folders(strFolder).Folders("All Public Folders").Folders("Backup Notifications").Folders(strProject)"

I was thinking a search might do it, but couldn't work out how to then make the search result into the variable I need.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This code from michael at VBOffice.net - finds folders .

this line returns the folder path:

Set Application.ActiveExplorer.CurrentFolder = m_Folder

you don't want to actually open it, so maybe this will work:

Set fld = m_Folder

And if it doesn't work with public folders... back to the drawing board. (Just checked - it finds PF.)
 

aaroncrt

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Thanks for the response Diane,

That won't work for this instance, as I'm already using an active selection for the email to be transferred. Basically I need to be able to click on an email and run the script, it prompts user which PF to transfer to and moves the email to it. This is to avoid the requirement of expanding the public folders and locating the folder etc to move to.

If there is a way to use the inputbox data as a search string for part of the public folder path, the result could then be used as the variable.

Something similar to the following perhaps? I just don't know enough VB to utilize it correctly.

Code:
Dim FldSub As Outlook.MAPIFolder 
 
For Each FldSub In nms.Folders(strFolder).Folders("All Public Folders").Folders("Projects").Folders
   If Left(FldSub.Name, 3) = strProject Then
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I'd try it. It looks like it should work, but so does your other code.
 

aaroncrt

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
FYI I got this working by searching the public folders for a match with the input variable.

Code:
ReDim sArray(0) As String 
 
If fld.Folders.Count Then
          
 For i = 1 To fld.Folders.Count
   If Left(fld.Folders(i).Name, 3) = strProject Then
       iElement = IIf(sArray(0) = "", 0, UBound(sArray) + 1)
       ReDim Preserve sArray(iElement) As String
       sArray(iElement) = fld.Folders(i).Name
   End If
 Next i
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
N Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
G Outlook VBA and Google Calendar ("Events") Outlook VBA and Custom Forms 1
J VBA Outlook : Subject line : Cut and Paste name to heading , number to very end of the body of Email Outlook VBA and Custom Forms 1
B Advanced Search in MS Outlook by VBA and SQL Outlook VBA and Custom Forms 2
K Outlook Archive to PST Files by Date Range VBA Script? Outlook VBA and Custom Forms 1
J Help Please!!! Outlook 2016 - VBA Macro for replying with attachment in meeting invite Outlook VBA and Custom Forms 9
S Find a cell value in excel using outlook vba Using Outlook 1
J Execute Add-In Button from VBA Outlook 2016 Outlook VBA and Custom Forms 1
J Open an outlook email by Subject on MS Access linked table with VBA Outlook VBA and Custom Forms 10
D create an html table in outlook custom form 2010 using vba in MsAccess Outlook VBA and Custom Forms 7
M Slow VBA macro in Outlook Outlook VBA and Custom Forms 5
T Outlook AntiSpam with VBA Outlook VBA and Custom Forms 1
F "Move to" O365 feature to Outlook client via VBA Outlook VBA and Custom Forms 4
B query outlook using vba Outlook VBA and Custom Forms 13
J VBA to switch Outlook online/offline Outlook VBA and Custom Forms 4
M VBA to change flag status in outlook contact item Outlook VBA and Custom Forms 3
T VBA outlook, detect priority emails Outlook VBA and Custom Forms 5
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
stephen li VBA Outlook send mail automatically by specified outlook mail box Outlook VBA and Custom Forms 1
S Outlook VBA Contacts Notes Outlook VBA and Custom Forms 0
O VBA to Run Font Change on Outlook Startup Outlook VBA and Custom Forms 4
P Outlook 2007 Email Categorization using VBA Outlook VBA and Custom Forms 1
O VBA or other solution for Outlook tasks to OneNote Outlook VBA and Custom Forms 0
S Automatically selecting folders and deleting messages in Outlook VBA Outlook VBA and Custom Forms 7
D Creating an outlook session from Access vba but run silently. With A specific profile Outlook VBA and Custom Forms 1
Diane Poremsky Outlook VBA: Use a Text File to Populate a ListBox New Slipstick.com Articles 0
C Saving Outlook attachments and links to attachments with VBA Outlook VBA and Custom Forms 2
Diane Poremsky Outlook VBA: Work with Open Item or Selected Item New Slipstick.com Articles 0
Diane Poremsky Working with VBA and non-default Outlook Folders New Slipstick.com Articles 0
C Outlook VBA to set current account Outlook VBA and Custom Forms 1
Diane Poremsky Use VBA to create an Outlook Search Folder for Sender New Slipstick.com Articles 0
F VBA routine to write new sub routine in outlook Outlook VBA and Custom Forms 0
D Change sender name outlook vba 2010 Custom Userform Outlook VBA and Custom Forms 1
G Adding a contact to Outlook with a custom form using Access VBA Outlook VBA and Custom Forms 1
C Outlook 2016 - converting Word VBA to default Outlook message Outlook VBA and Custom Forms 0
N VBA Script to Send Automatic Emails from Outlook 2010 Outlook VBA and Custom Forms 1
P Outlook 2016 from Excel 2016 VBA Using Outlook 1
N Get the seconds with userproperties.add method VBA Outlook Outlook VBA and Custom Forms 4
S Outlook 7 VBA macro for multiple filing Outlook VBA and Custom Forms 1
G Outlook 2010 VBA Coding Assistance please! Outlook VBA and Custom Forms 5

Similar threads

Top