Hello There, I was trying to invoke the following VB.Net code and run into an very non-descriptive error message. Any idea where i am doing it wrong
Dim Outlook As Microsoft.Office.Interop.Outlook.Application
Outlook = New Microsoft.Office.Interop.Outlook.Application
Outlook.Application.ActiveExplorer.WindowState = OlWindowState.olMinimized
Here is the Error: Invoke code: Exception has been thrown by the target of an invocation.
The code fails because there’s no Outlook window. New Microsoft.Office.Interop.Outlook.Application starts Outlook but doesn’t seem to create any (active) window.
Dim Outlook As Microsoft.Office.Interop.Outlook.Application
Outlook = New Microsoft.Office.Interop.Outlook.Application
Try
Outlook.Application.ActiveExplorer.WindowState = OlWindowState.olMinimized
Catch e As System.Exception
Console.WriteLine(e.Message)
End Try
Thank you for the response. There is no error now, but cant seem to open outlook. Could you please tell me how do i open outlook (in a minimized state). Appreciate your help
You will need Process.Start() to start Outlook and then use your code to minimize it.
Dim pOutlook As ProcessStartInfo = New ProcessStartInfo("OUTLOOK.EXE")
pOutlook.WindowStyle = ProcessWindowStyle.Minimized 'Seems to only minimize the Outlook splash window
Process.Start(pOutlook)
Dim mOutlook As Microsoft.Office.Interop.Outlook.Application
mOutlook = New Microsoft.Office.Interop.Outlook.Application
For index As Integer = 1 To 30
Try
mOutlook.ActiveExplorer.WindowState = OlWindowState.olMinimized 'Minimize the actual Outlook window
Exit For
Catch
Threading.Thread.Sleep(100)
End Try
Next
The above code will start Outlook and try to minimize Outlook (it will time out after about 3 seconds if there’s no Outlook window).