问题
I've amended some code I found which will allow me to create a ribbon button to search back sixty days.
Sub LastSixtyDays()
Dim myOlApp As New Outlook.Application
Dim tDate As Date
tDate = Now - 60
txtsearch = "received: (" & Format(tDate, "dd/mm/yyyy") & ".." & Format(Now, "dd/mm/yyyy") & ")"
myOlApp.ActiveExplorer.Search txtsearch, olSearchScopeCurrentFolder
Set myOlApp = Nothing
End Sub
which works perfectly, but how do i append "txtsearch" to the existing contents of the Instant Search box just like the clicking on the "has attachments", "subject", "from" in the search tab does?
So, if "From:bob" is in the Instant Search box, clicking my button will result in "From:bob received: (23/02/2015..24/04/2015)"
回答1:
The Outlook object model doesn't provide any property or method for that.
You may try to use CurrentView property of the Folder or Explorer class which returns a View object representing the current view. To obtain a View object for the view of the current Explorer, use Explorer.CurrentView instead of the CurrentView property of the current Folder object returned by Explorer.CurrentFolder. For example:
Sub GetView()
'Creates a new view
Dim objName As NameSpace
Dim objViews As Views
Dim objView As View
Set objName = Application.GetNamespace("MAPI")
Set objViews = objName.GetDefaultFolder(olFolderInbox).Views
'Return a view called Table View
Set objView = objViews.Item("Table View")
End Sub
The TableView class provides the Filter property which returns or sets a string value in DAV Searching and Locating (DASL) syntax, that represents the current filter for the view.
Private Sub FilterViewToLastWeek()
Dim objView As View
' Obtain a View object reference to the current view.
Set objView = Application.ActiveExplorer.CurrentView
' Set a DASL filter string, using a DASL macro, to show
' only those items that were received last week.
objView.Filter = "%lastweek(""urn:schemas:httpmail:datereceived"")%"
' Save and apply the view.
objView.Save
objView.Apply
End Sub
来源:https://stackoverflow.com/questions/29846339/how-do-i-capture-the-text-in-the-instant-search-box-in-outlook-2010-in-vba