Outlook advanced search macro

Cousin Excel 1 Reputation point
2025-01-09T06:23:42.2033333+00:00

Hi, I have received the following macro from chatGPT but it does not work.
I get no error message, parameters seem to be correct but I can not see even Advanced Search screen.
Where might I be making mistake or where can be wrong in the macro?
Thank you

Sub AdvancedSearchWithInput()

Dim inputValues As String

Dim values() As String

Dim startDate As String, endDate As String

Dim fromAddress As String, toAddress As String

Dim searchScope As String, searchQuery As String

' Prompt for input values

inputValues = InputBox("Enter Start Date, End Date, From, To in this order, separated by commas:", _

"Advanced Search", ",,,")

' Split the input into an array

values = Split(inputValues, ",")

' Validate input length

If UBound(values) <> 3 Then

MsgBox "Invalid input! Please provide 4 values separated by commas.", vbCritical, "Error"

Exit Sub

End If

' Assign values with default handling

startDate = Trim(values(0))

endDate = Trim(values(1))

fromAddress = Trim(values(2))

toAddress = Trim(values(3))

' Set default values for wildcards

If startDate = "*" Then startDate = "1900-01-01"

If endDate = "*" Then endDate = "2099-12-31"

If fromAddress = "*" Then fromAddress = ""

If toAddress = "*" Then toAddress = ""

' Build the search query

searchQuery = "urn:schemas:httpmail:datereceived >= '" & Format(startDate, "yyyy-mm-dd") & "' AND " & _

"urn:schemas:httpmail:datereceived <= '" & Format(endDate, "yyyy-mm-dd") & "'"

If fromAddress <> "" Then

searchQuery = searchQuery & " AND urn:schemas:httpmail:fromemail LIKE '%" & fromAddress & "%'"

End If

If toAddress <> "" Then

searchQuery = searchQuery & " AND urn:schemas:httpmail:toemail LIKE '%" & toAddress & "%'"

End If

' Define the search scope (default is Inbox)

searchScope = "'" & Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).FolderPath & "'"

' Perform the search

On Error Resume Next

Application.AdvancedSearch searchScope, searchQuery, True, "CustomSearch"

If Err.Number <> 0 Then

MsgBox "An error occurred while performing the search: " & Err.Description, vbCritical, "Error"

End If

On Error GoTo 0

End Sub

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,141 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.