Forum Discussion
Writing VBA Advanced Filter on Generic Mailbox
Good Afternoon,
I'm trying to use Outlook VBA / PowerShell COMObject to search for a specific email within a generic / shared mailbox that my team uses
When using VBA, this is my code.
Option Explicit
Sub MyEmailSearcher()
Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oSM As Outlook.Recipient
Dim oFolderInbox As Outlook.Folder
Dim oSearch As Outlook.Search
Dim oTable As Outlook.Table
Dim oRow As Outlook.Row
Dim sFilter As String
Dim sSearch As String
Dim check As Boolean
Set oApp = New Outlook.Application
Set oNS = oApp.GetNamespace("MAPI")
Set oSM = oNS.CreateRecipient("softwaredeployment-deploiementdelogiciel")
oSM.Resolve
If oSM.Resolved Then
Set oFolderInbox = oNS.GetSharedDefaultFolder(Recipient:=oSM, FolderType:=olFolderInbox)
sSearch = "OpenSSL"
sFilter = "urn:schemas:httpmail:subject LIKE '%" & sSearch & "%' OR " & _
"urn:schemas:httpmail:textdescription LIKE '%" & sSearch & "%'"
Set oSearch = Application.AdvancedSearch(oFolderInbox, sFilter, True, "GOPDKTP_Search")
Debug.Print (oSearch.Results.Count > 0)
End If
End Sub
The oSearch variable does get initialized by Application.AdvancedSearch but it doesn't get any results. PowerShell is basically the same except I initialize oApp as a COMObject of type Outlook.Application. When I run my PowerShell version, I get an error that simply says that the operation failed. When I generate a detailed error output, I get this for the error info:
Exception :
Type : System.Runtime.InteropServices.COMException
ErrorCode : -2147023281
TargetSite :
Name : CheckThrowException
DeclaringType : System.Management.Automation.ComInterop.ComRuntimeHelpers, System.Management.Automation, Version=7.2.7.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35
MemberType : Method
Module : System.Management.Automation.dll
Message : The operation failed.
Data : System.Collections.ListDictionaryInternal
Source : Microsoft Outlook
HResult : -2147023281
StackTrace :
at System.Management.Automation.ComInterop.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, ComMethodDesc method, Object[] args, UInt32 argErr)
at CallSite.Target(Closure , CallSite , ComObject , Object , Object , Boolean )
at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at System.Management.Automation.Interpreter.DynamicInstruction`5.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo : OperationStopped: (:) [], COMException
FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
InvocationInfo :
ScriptLineNumber : 1
OffsetInLine : 2
HistoryId : -1
Line : $Result = $oApp.AdvancedSearch($oFolder,$sFilter,$true)
PositionMessage : At line:1 char:2
+ $Result = $outlook.AdvancedSearch($oFolder,$sFilter,$true)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
About my environment
Outlook 365 Semi-Annual channel, version 16.0.0.14931
Exchange version 15.20.5813.18
Cached Mode is managed via GPO/Intune MDM, set to Enabled.
The message does exist in the target mailbox. When I manually search in the Outlook app, no trouble finding it.
Any ideas?