none
new outlook.application not working if outllok is opened RRS feed

  • Question

  • Hi

    I'm trying to access open outlook instance from vb.net project.
            On Error Resume Next
            OutlookApp = GetObject(, "Outlook.Application")
            If OutlookApp Is Nothing Then
                OutlookApp = New Outlook.Application
            End If

    On the line "OutlookApp = GetObject(, "Outlook.Application")" I'm getting 

    Exception thrown: 'System.Exception' in Microsoft.VisualBasic.dll Cannot create ActiveX component

    If I'm trying to use "OutlookApp = New Outlook.Application" when the outlook is open I'm getting the same error, but if the outlook is closed it working fine.

    Is there another way to check if the outlook is open and get an instance to its object?

    My environment:
    win 10 64 bit
    office 365 64 bit
    Visual Studio Community 2019 ver 4.8.03761


    Leon

    Tuesday, February 25, 2020 1:04 PM

Answers

  • Found it was because Visual Studio was open as administrator and outlook was not.
    Set both to normal or
    both to administrator -> fires perfectly.
    • Marked as answer by Leon P Tuesday, February 25, 2020 2:55 PM
    Tuesday, February 25, 2020 2:55 PM

All replies

  • I've found this:

        Function GetApplicationObject() As Outlook.Application
            Try
                Dim application As Outlook.Application
    
                'Check If there Is an Outlook process running. 
                If Process.GetProcessesByName("OUTLOOK").Count() > 0 Then
    
                    ' If so, use the GetActiveObject method to obtain the process and cast it to an Application object. 
                    application = DirectCast(Marshal.GetActiveObject("Outlook.Application"), Outlook.Application)
                Else
    
                    ' If not, create a new instance of Outlook and log on to the default profile. 
                    application = New Outlook.Application()
                    Dim ns As Outlook.NameSpace = application.GetNamespace("MAPI")
                    ns.Logon("", "", Missing.Value, Missing.Value)
                    ns = Nothing
                End If
    
                ' Return the Outlook Application object. 
                Return application
    
            Catch ex As system.Exception
                MsgBox(Err.Description)
            End Try
        End Function
    

    And on line "application = DirectCast(Marshal.GetActiveObject("Outlook.Application"), Outlook.Application)" 
    got another Error:
    Operation unavailable (-  HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))

    Tuesday, February 25, 2020 1:39 PM
  • Found it was because Visual Studio was open as administrator and outlook was not.
    Set both to normal or
    both to administrator -> fires perfectly.
    • Marked as answer by Leon P Tuesday, February 25, 2020 2:55 PM
    Tuesday, February 25, 2020 2:55 PM