Windows startup -> launches my application -> this application should launch outlook in hidden mode (not in taskbar, just outlook.exe running in the back / tray)
Tried a bunch of workarounds with different Sleep waits (I know, ain't a good practice), but nothing that really ensures that outlook will be launched hidden no matter the timing. - The code belows has every workaround imaginable to me, nothing worked bulletproof.
Private Sub OutlookCheck()
Dim isOpen As Boolean = False
Threading.Thread.Sleep(6000)
For Each p As Process In Process.GetProcessesByName("outlook")
If p.ProcessName.Contains("OUTLOOK") Then
isOpen = True
End If
Next
If Not isOpen Then
Try
PrintAndLog("Outlook not running yet")
Threading.Thread.Sleep(6000)
StartOutlookHidden()
Catch ex As Exception
ErrMsg(ex.Message)
End Try
Else
PrintAndLog("Outlook already running, trying to close")
For Each p As Process In Process.GetProcessesByName("outlook")
If p.ProcessName.Contains("OUTLOOK") Then
p.Kill()
PrintAndLog("Outlook process with ID: " & p.Id & " found and killed. Starting routine for hidden Outlook")
Exit For
End If
Next
Threading.Thread.Sleep(6000)
StartOutlookHidden()
End If
End Sub
Public outlookhWnd As IntPtr
Private outlookHideTicks As Short = 5
Private Sub StartOutlookHidden()
Try
Using regKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Office\16.0\Outlook\Preferences", True)
regKey.SetValue("MinToTray", 1, Microsoft.Win32.RegistryValueKind.DWord)
regKey.Close()
End Using
Catch ex As Exception
ErrMsg(ex.Message)
End Try
Dim pset As ProcessStartInfo = New ProcessStartInfo
With pset
.FileName = "outlook.exe"
.WindowStyle = ProcessWindowStyle.Minimized
.CreateNoWindow = True
End With
Using p As Process = New Process
With p
.StartInfo = pset
.Start()
End With
Threading.Thread.Sleep(6000) 'Wait for outlook to open
outlookhWnd = p.MainWindowHandle
ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_HIDE)
End Using
Dim i As Threading.Thread = New Threading.Thread(Sub() AddidtionalHide())
i.SetApartmentState(Threading.ApartmentState.STA)
i.Start()
PrintAndLog("Hidden Outlook started")
End Sub
Private Sub AddidtionalHide()
ShowWindow(outlookhWnd, SHOW_WINDOW.SW_HIDE)
Threading.Thread.Sleep(2000)
For Each p As Process In Process.GetProcessesByName("outlook")
ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_HIDE)
Next
outlookHideTicks -= 1
If outlookHideTicks >= 1 Then
AddidtionalHide()
End If
End Sub
I want outlook to open along with my application, but hidden in the background. Since outlook doesn't bother about processproperties and the initial window handle is only used when it opens the mainwindow(usually inbox) and not before, I find it hard to get a code that'll work on all machines.
Simply create an instance of the Outlook.Application
object and keep a reference to it.