Cannot find the error in code for identifying if email account exist

53 views Asked by At

In the RPA software Blue Prism, I am trying to write some VB.Net code for identifying if an account (i.e. Account_Display_Name) is configured to the desktop Outlook application (including shared mailboxes). The code utilizes Microsoft.Office.Interop.Outlook.dll, and the internal compiler does not identify any errors. However, when I try to run the code, I get the error "Object variable or With block variable not set".

I cannot find what is causing the issue, and would really appreciate your support. Perhaps there is a better way altogether for achieving what I want...

Dim olApp As Object
Dim olNamespace As Object
Dim Account_Exist As Boolean
Dim Account_Display_Name As String

olApp = CreateObject("Outlook.Application")
olNamespace = olApp.GetNameSpace("MAPI")
Account_Exist=False
 
If Not String.IsNullOrEmpty(Account_Display_Name) Then
    Dim aa As List(Of Account) = olNamespace.Accounts.Cast(Of Account).ToList()
    For Each x As Account In aa
        If x.DisplayName = Account_Display_Name Then
             Account_Exist=True
             Exit For           
        End If
    Next
End If

I have tried to put the code into online compilers to see if I can get some leads, but to no prevail.

1

There are 1 answers

0
ClearlyClueless On

The logic in your loop doesn't appear that it will ever execute. Is that intentional? Dim Account_Display_Name as String will initialize to string.empty and when it hits the if statement, String.IsNullOrEmpty(Account_Display_Name) will return true and then be negated by your not and your inner block will be skipped. (assuming this is a complete excerpt and nothing was ommitted for brevity. If someone was removed I would encourage you to edit your OP and update it with the complete code sample.)

With the above in mind, we know that the for loop logic is not getting hit so we know the issue is likely occurring at olApp.getNameSpace("MAPI"). The most likely culprit is olApp is nothing while you are trying to reference one of its functions.

You can confirm which with a break point and stepping through the debugger.

I have tried to put the code into online compilers to see if I can get some leads, but to no prevail

This is indeed the trouble with logic bugs as syntactically, your code appears to be correct.