VBA Question

ChrisFoster

Board Regular
Joined
Jun 21, 2019
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I hope I'm posting in the correct section, please just move if I have.

Please can someone write me some code for VBA to use in Outlook?
I need to be able to run a macro to search the current folder (not whole mailbox) for all read emails. This is in place of manually clicking search, typing isread:yes, changing the folder to 'Current Folder'.

Kind regards,

Chris
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
The following VBA macro will search the current folder for all unread mails. It behaves as if you have typed the query string in the Instant Search user interface and clicked Search button.
VBA Code:
Public Sub SearchForUnreadMails()
    If Application.ActiveExplorer.CurrentFolder.DefaultItemType = olMailItem Then
        Dim objExpl As Outlook.Explorer
        Set objExpl = Application.ActiveExplorer
        objExpl.Search "isread:yes", olSearchScopeCurrentFolder
    End If
End Sub
However, the above VBA macro will only displays the search result in the Outlook GUI. To retrieve information about each unread mail (Body, Subject, or Attachment(s)), you can try the following macro:
VBA Code:
Public Sub GetUnreadMailsInCurrentFolder()
    Dim objFolder As Outlook.Folder
    Set objFolder = Application.ActiveExplorer.CurrentFolder
    Dim strFilter As String
    strFilter = "@SQL=" & Quote("urn:schemas:httpmail:read") & "=0"
    Dim colItems As Outlook.Items
    Set colItems = objFolder.Items.Restrict(strFilter)
    If colItems.Count > 0 Then
        Dim objMail As Outlook.MailItem
        Set objMail = colItems.GetFirst
        Do While Not objMail Is Nothing
            'Do something with the search result
            Debug.Print objMail.Subject 'Print the mail subject
            Set objMail = colItems.GetNext
        Loop
    End If
End Sub

Private Function Quote(ByVal Text As String) As String
    Quote = Chr(34) & Text & Chr(34)
End Function
You may modify the code to suit your need.
 
Upvote 0
The following VBA macro will search the current folder for all unread mails. It behaves as if you have typed the query string in the Instant Search user interface and clicked Search button.
VBA Code:
Public Sub SearchForUnreadMails()
    If Application.ActiveExplorer.CurrentFolder.DefaultItemType = olMailItem Then
        Dim objExpl As Outlook.Explorer
        Set objExpl = Application.ActiveExplorer
        objExpl.Search "isread:yes", olSearchScopeCurrentFolder
    End If
End Sub

Thanks - for this code it states that macros are disabled for this project. Any ideas? I have macros enabled on Outlook.
 
Upvote 0
From the Developer tab on the ribbon, click the Macro Security button -> Macro Settings and check to see if the option "Enable all macros..." is checked.
1686127615501.png
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top