84294

outlook vba select messages in sub-folder

Question:

Outlook 2007 is configured with two email accounts:

<ul><li>Account#1: Hotmail</li> <li>Account#2: Gmail</li> </ul>

I would like to create a macro named simulating a user doing the following:

<ul><li>Left click on a within either the hotmail or gmail account.</li> <li>Highlight all messages within the folder previously selected.</li> <li>display a messageBox with the number of emails selected from this folder</li> </ul>

I have tried several methods to define the folder, but its not working. My suspicion is it would work on the default PST, but that isn't what I'm using. Even tried using the method below to identify the specific folder I want to use. It does print out a path, but I am not able to use that as a variable value directly.

Any suggestions?

=== Information ===

The following macro was used to obtain information about the account & folder locations: <a href="http://www.gregthatcher.com/Scripts/VBA/Outlook/GetFolderInfo.aspx" rel="nofollow">http://www.gregthatcher.com/Scripts/VBA/Outlook/GetFolderInfo.aspx</a>

<ol><li>Hotmail <ul><li>Name: aaaaa</li> <li>FolderPath: \@hotmail.com\aaaaa</li> </ul></li> </ol>

-

<ol start="2"><li>Gmail <ul><li>Name: bbbbb</li> <li>FolderPath: \@gmail.com\bbbbb </li> </ul></li> </ol><hr />' please add your values for Const emailAccount and Const folderToSelect ' To begin, launch: start_macro ' ' the macro will loop all folders and will check two things , folder name and account name, ' when both are matched , will make that folder the active one , then will select all emails ' from it and at final will issue number of selected items no other References are required ' than default ones Option Explicit #If VBA7 Then Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems #Else Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems #End If ' please provide proper values for email account and folder name Const emailAccount = "username@hotmail.com" Const folderToSelect = "folder" ' declare some public variables Dim mySession As Outlook.NameSpace Dim myExplorer As Outlook.Explorer Dim mySelection As Outlook.Selection Dim my_folder As Outlook.folder Sub start_macro() Dim some_folders As Outlook.Folders Dim a_fld As Variant Dim fld_10 As Outlook.folder Set mySession = Application.Session Set some_folders = mySession.Folders For Each a_fld In some_folders Set fld_10 = a_fld Call loop_subfolders_2(fld_10) Next a_fld End Sub Sub final_sub() If Not (my_folder Is Nothing) Then Set myExplorer = Application.ActiveExplorer Set Application.ActiveExplorer.CurrentFolder = my_folder Call select_all_items(my_folder) Else MsgBox "There is no folder available for specified account !!!" End If End 'end the macro now End Sub Sub loop_subfolders_2(a_folder As Outlook.folder) Dim col_folders As Outlook.Folders Dim fld_1 As Outlook.folder Dim arr_1 As Variant Set col_folders = a_folder.Folders For Each fld_1 In col_folders If Left(fld_1.FolderPath, 2) = "\\" Then arr_1 = Split(fld_1.FolderPath, "\") 'Debug.Print fld_1.Name & vbTab & arr_1(2) & vbTab & fld_1.FolderPath If InStr(LCase(emailAccount), "@gmail.com") > 0 Then If LCase(folderToSelect) = LCase(fld_1.Name) Then If LCase(emailAccount) = LCase(arr_1(2)) Or arr_1(2) = "Personal Folders" Then Set my_folder = fld_1 Call final_sub Else Call loop_subfolders_2(fld_1) End If Else Call loop_subfolders_2(fld_1) End If Else If LCase(folderToSelect) = LCase(fld_1.Name) And LCase(emailAccount) = LCase(arr_1(2)) Then Set my_folder = fld_1 Call final_sub Else Call loop_subfolders_2(fld_1) End If End If End If Next fld_1 End Sub Sub select_all_items(my_folder As Outlook.folder) Dim my_items As Outlook.Items Dim an_item As MailItem Dim a As Long, b As Long Set my_items = my_folder.Items b = my_items.Count DoEvents 'sleep 2000 Set mySelection = myExplorer.Selection If CLng(Left(Application.Version, 2)) >= 14 Then On Error Resume Next ' there are other folders that do not contains mail items For Each an_item In my_items If myExplorer.IsItemSelectableInView(an_item) Then myExplorer.AddToSelection an_item Else End If Next an_item On Error GoTo 0 Else myExplorer.Activate If b >= 2 Then For a = 1 To b - 1 SendKeys "{DOWN}" 'Sleep 50 Next a For a = 1 To b - 1 SendKeys "^+{UP}" ' 'Sleep 50 Next a End If DoEvents 'sleep 2000 End If Set my_items = Nothing Set mySelection = myExplorer.Selection MsgBox mySelection.Count End Sub

Answer1:

does this one work?

Function GetFolder(ByVal FolderPath As String) As Outlook.folder Dim TestFolder As Outlook.folder Dim FoldersArray As Variant Dim i As Integer On Error GoTo GetFolder_Error If Left(FolderPath, 2) = "\\" Then FolderPath = Right(FolderPath, Len(FolderPath) - 2) End If 'Convert folderpath to array FoldersArray = Split(FolderPath, "\") Set TestFolder = Application.Session.Folders.item(FoldersArray(0)) If Not TestFolder Is Nothing Then For i = 1 To UBound(FoldersArray, 1) Dim SubFolders As Outlook.Folders Set SubFolders = TestFolder.Folders Set TestFolder = SubFolders.item(FoldersArray(i)) If TestFolder Is Nothing Then Set GetFolder = Nothing End If Next End If 'Return the TestFolder Set GetFolder = TestFolder Exit Function GetFolder_Error: 'MsgBox ("Ordner für verschieben nicht gefunden") Set GetFolder = Nothing Exit Function End Function

for me this works with all Folders, no matter if Primary or other box (but all of them being Exchange, but I do not think this maters)

e.g. These work:

Set mailitem.SaveSentMessageFolder = GetFolder(mailitem.SentOnBehalfOfName & "\inbox") Dim Subfolder As Outlook.MAPIFolder Set Subfolder = GetFolder(olfolder.FullFolderPath & "\erledigt") Dim Subfolder As Outlook.MAPIFolder Set Subfolder = GetFolder("someaccount\inbox")

Recommend

  • Batch file to delete all folders in a directory except the newest folder
  • Xamarin iOS debugger not hitting breakpoints
  • Git cleanup/garbage collection on remote VSO git repository
  • Android - Call default browser with and redirect to a designated url
  • Persistent stty Settings?
  • Visual Studio - PathTooLongException even when the path length is less than 260 characters
  • What is the best way to debug Bootstrap.groovy?
  • Python adding lots of things to PATH. How do I stop?
  • Mocha throws unexpected token error for ES6 object spread operator
  • How can I prevent the need to copy strings passed to a avr-gcc C++ constructor?
  • Deploying a CodeRush plugin from the Community Site
  • Local Development, Apache vs Developer - file permissions
  • ckeditor and jquery UI dialog not working
  • Less Conflicting Session Manager for Zope 2
  • mave 3.2 not able to access local nexus instance return 502 code
  • Can I use AllJoyn Framework for Wifi Direct in iOS?
  • redirect_to root_url and return unless @user.activated
  • Cloud Code function running twice
  • Can't remove headers after they are sent
  • How does document.ready work with angular element directives?
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • Webgrid not refreshing after delete MVC
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Atlas images wrong size on iPad iOS 9
  • $wpdb not working in file of WordPress plugin
  • how to adjust image in a panel in Java swing?
  • Display issues when we change from one jquery mobile page to another in firefox
  • Different response to non-authenticated users and AJAX calls
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • Data Validation Drop Down Box Arrow Disappearing
  • How do you join a server to an Active Directory (domain)?
  • JTable with a ScrollPane misbehaving
  • How does Linux kernel interrupt the application?
  • Reading document lines to the user (python)