在 windows 启动时启动隐藏的 outlook

Launch outlook hidden on windows startup

Windows 启动 -> 启动我的应用程序 -> 这个应用程序应该以隐藏模式启动 outlook(不是在任务栏中,只是 outlook.exe 运行 在后面/托盘)

尝试了一系列具有不同睡眠等待时间的变通方法(我知道,这不是一个好的做法),但没有任何方法可以真正确保 outlook 将在任何时间隐藏启动。 - 下面的代码有我能想到的所有解决方法,没有任何东西是防弹的。

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.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

我希望 outlook 与我的应用程序一起打开,但隐藏在后台。由于 outlook 不关心进程属性,并且初始 window 句柄仅在打开主window(通常是收件箱)时使用,而不是之前,我发现很难获得可以工作的代码在所有机器上。

只需创建 Outlook.Application 对象的实例并保留对它的引用。