如何遍历选中的Outlook邮件文件夹子文件夹并修改名称?
批量修改Outlook邮件文件夹名称:遍历选中文件夹子文件夹的问题与解决
我希望从选中的邮件(子)文件夹开始,批量处理一组邮件文件夹,目标是修改每个文件夹名称的前几个字符。

以下是我编写的VB代码,但在获取选中文件夹的步骤(Set fld = ns.Selection)处执行失败:
Sub sf_Rename_email_folders2() Dim objPane As NavigationPane Dim CurrentModule As NavigationModule Dim lngCounter As Long Dim ns As Outlook.Namespace Dim fld As Outlook.MAPIFolder Dim objUnknown As Object Dim mail As Outlook.MailItem Dim prop As Outlook.UserProperty Set objPane = Application.ActiveExplorer.NavigationPane Set CurrentModule = objPane.CurrentModule Set ns = Application.GetNamespace("MAPI") ' Set fld = ns.GetDefaultFolder(olFolderInbox) Set fld = ns.Selection 'Fails here If CurrentModule.NavigationModuleType = olModuleMail Then For Each objUnknown In fld.Items lngCounter = lngCounter + 1 ' If TypeOf objUnknown Is Folder Then 'Folder is my guess ' Debug.Print lngCounter ' Debug.Print objUnknown.Type ' Debug.Print objUnknown.Name ' End If Next End If End Sub
请问如何循环遍历选中文件夹的子文件夹名称?
更新内容
参考回复后,我成功实现了对父文件夹为“Shopping”的邮件子文件夹的重命名操作:这些子文件夹原名称以3位数字为前缀(例如“123 Description”),我需要将其改为4位数字前缀。以下是仅需执行一次的程序代码,其中包含了确保修改的是邮件文件夹的检查逻辑:
Sub sf_Rename_email_folders3() ' Purpose : to Rename shopping email folders names by prefixing each one with a zero "0". ' This extends the shopping index numbering to 9999 which I am unlikely to exceed. ' ' NOTE: this is expected to be a ONE-TIME-ONLY procedure - but you never know... ' ' Needs : Current object group to be email and to have a folder selected. The folder must be in Home Situations ' to prevent damaging the mail folder system. ' ' Activation : by hand, once only. Dim objMod As NavigationModule Dim f0 As Folder, sf1 As Folder, sf2 As Folder Dim oItem As Object Dim oMail As Outlook.MailItem Dim Folder_save As Folder Dim SF_Is_0_9 As Boolean ' Just select the group requiring renaming. Don't get that wrong... Set objMod = Application.ActiveExplorer.NavigationPane.CurrentModule ' Ensure a mail folder is selected. If objMod.NavigationModuleType <> olModuleMail Then MsgBox "First select the parent email folder of the group to have indices prefixed with zero" Exit Sub End If ' Begin at the selected folder Set f0 = Application.ActiveExplorer.CurrentFolder Debug.Print "Processing folder: " & f0.Name ' Iterate through first group sub-folders For Each sf1 In f0.Folders 'check each subfolder ' * Until the end of list Debug.Print " subfolder: " & sf1.Name & " contains..." ' Iterate through the second list of folders ' NB it's these folder names that need a new zero prefix For Each sf2 In sf1.Folders ' * Until the end of list ' Check that first character in name is 0-9 SF_Is_0_9 = IsNumeric(Left(sf2.Name, 1)) If SF_Is_0_9 Then ' o Begins with 0-9 ' Prefix with zero ' sf2.Name = "0" & sf2.Name 'commented out for safety. Use once only. Debug.Print sf2.Name Else ' o Not 0-9 ' User message and stop processing MsgBox ("Fault: folder does not begin with 0-9: " & sf1.Name & Chr$(10) & sf2.Name) End If Next sf2 Next sf1 Debug.Print "End of Processing" End Sub
内容的提问来源于stack exchange,提问作者theHeatingEngineer
相关产品推荐
相关产品推荐

