2017-07-20 101 views
0

我正在爲使用Visual Studio 2015的Outlook 2016編寫加載項。我向內置的新建郵件選項卡添加了一個按鈕。點擊後,它會將「未加密」一詞添加到主題行的末尾,然後發送電子郵件。VB .NET Outlook 2016加載項主題行

只要用戶在輸入主題後標籤出主題行字段,這工作正常。但是,如果您鍵入主題,然後立即單擊該按鈕,則會刪除主題行,並用「解密」替換它。

但是,當我在調試中逐步完成時,它可以正常工作 - 即使我沒有標記出主題行,它也會保留現有文本。我認爲在更新郵件項目的Subject屬性時存在一些延遲,但我手動放置了20秒的延遲,並且如果我沒有在調試中逐步完成,它仍然消除了主題行。

我在這裏不知所措。有沒有辦法檢查主題行文本框?或者其他方式來抓取文本,即使用戶沒有標籤出來?

任何幫助,將不勝感激!

Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click 
    ' Get the Application object 
    Dim application As Outlook.Application = Globals.ThisAddIn.Application 

    ' Get the active Inspector object and check if is type of MailItem 
    Dim inspector As Outlook.Inspector = application.ActiveInspector() 
    Dim mailItem As Outlook.MailItem = TryCast(inspector.CurrentItem, Outlook.MailItem) 
    If mailItem IsNot Nothing Then 
     If mailItem.EntryID Is Nothing Then 
      If Not IsNothing(mailItem.Subject) AndAlso ((mailItem.Subject.Contains(" unencrypt")) OrElse (mailItem.Subject.Contains("unencrypt "))) Then 
       mailItem.Subject = mailItem.Subject 
      'ElseIf IsNothing(mailItem.Subject) Then 
       'System.Threading.Thread.Sleep(20000) 
       'mailItem.Subject = mailItem.Subject + " unencrypt" 
      Else 
       mailItem.Subject = mailItem.Subject + " unencrypt" 
      End If 
      If Not IsNothing(mailItem.To) AndAlso mailItem.To.ToString().Trim <> "" Then 
       mailItem.Send() 
      Else 
       MessageBox.Show("We need to know who to send this to. Make sure you enter at least one name.", "Microsoft Outlook", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) 
      End If 
     End If 
    End If 
End Sub 

編輯: 梅德的回答讓我在那裏我需要的,但對於其他人不熟悉的Windows API我添加下面的代碼,然後簡單地稱之爲從我的原代碼,而不是使用的MailItem的GetSubject功能。主題屬性。

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ 
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _ 
             ByVal childAfter As IntPtr, _ 
             ByVal lclassName As String, _ 
             ByVal windowTitle As String) As IntPtr 
End Function 
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ 
Private Shared Function FindWindow(ByVal lclassName As String, _ 
            ByVal lWindowName As String) As IntPtr 
End Function 
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ 
Private Shared Function GetWindowText(ByVal hWnd As IntPtr, _ 
             ByVal lpString As StringBuilder, _ 
             ByVal nMaxCount As Integer) As Integer 
End Function 
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ 
Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer 
End Function 

Private Function GetSubject(inspector As Outlook.Inspector) As String 
    Try 
     Dim inspectorHandle As IntPtr = FindWindow("rctrl_renwnd32", inspector.Caption) 
     Dim windowLevel2Handle As IntPtr = FindWindowEx(inspectorHandle, IntPtr.Zero, "AfxWndW", "") 
     Dim windowLevel3Handle As IntPtr = FindWindowEx(windowLevel2Handle, IntPtr.Zero, "AfxWndW", "") 
     Dim windowLevel4Handle As IntPtr = FindWindowEx(windowLevel3Handle, IntPtr.Zero, "#32770", "") 
     Dim SubjectHandle As IntPtr = FindWindowEx(windowLevel4Handle, IntPtr.Zero, "Static", "S&ubject") 
     Dim SubjectTextBoxHandle As IntPtr = FindWindowEx(windowLevel4Handle, SubjectHandle, "RichEdit20WPT", "") 
     Dim length As Integer = GetWindowTextLength(SubjectTextBoxHandle) 
     Dim sb As New StringBuilder(length + 1) 
     GetWindowText(SubjectTextBoxHandle, sb, sb.Capacity) 

     Return sb.ToString() 
    Catch 
     Return "" 
    End Try 

End Function 

回答

0

的重要組成部分,是主題編輯框中需要失去焦點的OOM覺察到的變化。

您可以使用輔助功能API或原始Windows API訪問編輯框的內容,也可以嘗試將其他檢查器控件(如郵件正文編輯器)集中。

+0

我是一個Windows API的完全新手,但Spy ++能夠讓我得到我需要的東西,並且一些谷歌搜索向我展示瞭如何使用它。感謝指針! –