dougmarkham
Active Member
- Joined
- Jul 19, 2016
- Messages
- 252
- Office Version
- 365
- Platform
- Windows
Hi Folks,
Basic issue:
I have a requirement to rename many outlook folders/sub-folders and I thought I'd found the perfect outlook VBA code; however, I have two issues with it 1) it's changing the case of my outlook folders to lowercase 2) It's doing part matches.
Detail
The code below utilizes a 'find column and replace column' from an excel worksheet to stipulate the find / replace text, and searches the outlook folders looking for find text to replace.
However, there are two problems with the code which are beyond my outlook VBA experience:
Problem 1) The Function below converted the text in 'all' my folders into lowercase i.e., regardless of whether they were in the find / replace excel columns or not (see LCase).
Problem 2) If the find and replace text are similar, it doesn't search the whole 'find' folder string: e.g., if the find text = Meeting and the replace text = Meeting1, it keeps recursively renaming such that Meeting becomes Meeting1-->Meeting11-->Meeting111......Meeting111111111111111111 etc., till I escape out.
The code:
Would anyone please be willing to help me modify this code so that
a) it doesn't modify the case during the rename operation and
b) it doesn't search for the find-text in part of a folder-string but only looks at the whole folder-string during the find operation?
Kind regards,
Doug.
Basic issue:
I have a requirement to rename many outlook folders/sub-folders and I thought I'd found the perfect outlook VBA code; however, I have two issues with it 1) it's changing the case of my outlook folders to lowercase 2) It's doing part matches.
Detail
The code below utilizes a 'find column and replace column' from an excel worksheet to stipulate the find / replace text, and searches the outlook folders looking for find text to replace.
However, there are two problems with the code which are beyond my outlook VBA experience:
Problem 1) The Function below converted the text in 'all' my folders into lowercase i.e., regardless of whether they were in the find / replace excel columns or not (see LCase).
Problem 2) If the find and replace text are similar, it doesn't search the whole 'find' folder string: e.g., if the find text = Meeting and the replace text = Meeting1, it keeps recursively renaming such that Meeting becomes Meeting1-->Meeting11-->Meeting111......Meeting111111111111111111 etc., till I escape out.
The code:
VBA Code:
Public strFind, strReplace As String
Private Sub FindReplaceWordsinFolderNames()
Dim objFolders As Outlook.Folders
Dim objFolder As Outlook.Folder
Dim strFilepath
Dim xlApp As Object 'Excel.Application
Dim xlWkb As Object ' As Workbook
Dim xlSht As Object ' As Worksheet
Dim rng As Object 'Range
Set xlApp = CreateObject("Excel.Application")
strFilepath = "C:\Users\RenameOutlookFolders.xlsm"
If strFilepath = False Then
xlApp.Quit
Set xlApp = Nothing
Exit Sub
End If
Set xlWkb = xlApp.Workbooks.Open(strFilepath, ReadOnly:=True, Password:="mypassword123")
Set xlSht = xlWkb.Worksheets("OutlookFolders")
Dim iRow As Integer
iRow = 2
Set objFolders = Outlook.Application.Session.Folders("Douglas.Markham@thecompany.com").Folders
'You need to input the specific words for find and replace
While xlSht.Cells(iRow, 1) <> ""
strFind = xlSht.Cells(iRow, 4)
strReplace = xlSht.Cells(iRow, 5)
For Each objFolder In objFolders
Call ProcessFolders(objFolder)
iRow = iRow + 1
Next
MsgBox "Complete!", vbExclamation, "Rename Folders"
End Sub
Private Sub ProcessFolders(ByVal objCurrentFolder As Outlook.Folder)
Dim objSubfolder As Outlook.Folder
On Error Resume Next
If InStr(LCase(objCurrentFolder.Name), LCase(strFind)) > 0 Then
'Find and replace the specific words
objCurrentFolder.Name = Replace(LCase(objCurrentFolder.Name), LCase(strFind), strReplace)
End If
'Process all folders recursively
If objCurrentFolder.Folders.Count > 0 Then
For Each objSubfolder In objCurrentFolder.Folders
Call ProcessFolders(objSubfolder)
Next
End If
End Sub
Would anyone please be willing to help me modify this code so that
a) it doesn't modify the case during the rename operation and
b) it doesn't search for the find-text in part of a folder-string but only looks at the whole folder-string during the find operation?
Kind regards,
Doug.