VB.NET 删除消息框后运行不同 - Outlook 应用程序

VB.NET runs differently when message box removed - Outlook Application

我目前正在开发一个 Outlook 应用程序,它在测试期间似乎运行良好。

此代码似乎在 MSGBox("ASDFG") 就位时有效 (rsts.Count returns 1),但一旦删除它,语句 returns 0。我试图通过添加 Debug.Print 来超越它,看看是否有任何区别,但我仍然发现当 MSGBox 就位时,代码只会 运行 正确。

我什至添加了一个 Timer1 间隔来每秒重新 运行 脚本,如果它 returns 0.

Function myThread()
    Dim oApp As Outlook.Application = CreateObject("Outlook.application")
    Dim sch As Outlook.Search
    Dim rsts As Outlook.Results
    Dim i As Integer
    Dim myTaskSearch As String = ToolStripStatusLabel2.Text
    Dim strF As String = "urn:schemas:mailheader:subject LIKE '%Task: " & myTaskSearch & "%'"
    Const strS As String = "Inbox"

    Try
        sch = oApp.AdvancedSearch(strS, strF)
        rsts = sch.Results
        MsgBox("ASDFG")
        Debug.Print(sch.Results.ToString)
        Debug.Print("'" & myTaskSearch & "'")

        If rsts.Count = 0 Then
            Debug.Print(rsts.Count)
            Timer1.Interval = 1000
            Timer1.Start()
        End If
        For i = 1 To rsts.Count
            Debug.Print(i)
            Timer1.Stop()
            TabControl1.TabPages.Add(i)
            'rsts.Item(i).Body
            'rsts.Item(i).SenderName
        Next
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Function

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    myThread()
End Sub

使用 MSGBox


没有 MSGBox + 调试

添加搜索完成时要调用的事件处理程序。正如 GSerg 所说,AdvancedSearch 是异步的,因此当您的代码打印计数时它仍然是 运行。

有关代码示例,请参阅 the documentation