VB.NET Outlook.SelectNamesDialog zu suchender Name in Suchfeld eintragen

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von JTrum.

    VB.NET Outlook.SelectNamesDialog zu suchender Name in Suchfeld eintragen

    Wenn ein Name mit resolveAll() nicht aufgelöst werden kann, will ich mit dem SelectNamesDialog den Namen entsprechend suchen und auswählen. Soweit so gut. Habe ein Beispiel gefunden, wo der zu suchende Name in Outlook.OlRecipientSelectors.olShowTo geschrieben wird.Wie kriege ich es hin, dass das Suchfeld oben in SelectNamesDialog mit dem zu suchenden Namen gefüllt wird. Wie heißt das Suchfeld? Wie spreche ich es an?

    VB.NET-Quellcode

    1. Dim objOutlook As Outlook.Application = GetApplicationObject()
    2. Dim myRecipient As Outlook.Recipient
    3. Dim myRecipients As Outlook.Recipients
    4. Dim myMail As Outlook.MailItem
    5. Try
    6. myMail = CType(objOutlook.CreateItem(Outlook.OlItemType.olMailItem), _
    7. Outlook.MailItem)
    8. myRecipients = myMail.Recipients
    9. myRecipients.Add("Mayer, Max")
    10. myRecipients.ResolveAll()
    11. For Each myRecipient In myRecipients
    12. If myRecipient.Resolved Then
    13. Debug.Print(myRecipient.Address & " - " & myRecipient.Name)
    14. userName = myRecipient.Name
    15. Exit For
    16. Else
    17. Debug.Print("Alias für: " & userName & " konnte nicht aufgelöst werden!")
    18. For i As Integer = 1 To myRecipients.Count
    19. Dim snd As Outlook.SelectNamesDialog = _
    20. objOutlook.Session.GetSelectNamesDialog()
    21. snd.Recipients.Add(userName)
    22. snd.NumberOfRecipientSelectors = Outlook.OlRecipientSelectors.olShowTo
    23. snd.AllowMultipleSelection = False
    24. snd.Display()
    25. If Not (snd.Recipients.ResolveAll()) Then
    26. myRecipients.Remove(i)
    27. userName = ""
    28. Exit For
    29. Else
    30. myRecipients.Remove(i)
    31. myRecipients.Add(snd.Recipients(1).Name)
    32. userName = snd.Recipients(1).Name
    33. Exit For
    34. End If
    35. Next
    36. End If
    37. Next
    38. Catch ex As Exception
    39. Debug.Print(ex.ToString)
    40. End Try
    Dann doch nochmal eine Frage.
    Habe jetzt das Beispiel unter vboffice.net/en/developers/get…ialog-pre-fill-search-box
    in mein VB.net Projekt eingebaut, wobei ich an einer Stelle nicht weiterkomme.

    VB.NET-Quellcode

    1. Public Sub DisplayDialog(FindThisName As String)
    2. On Error GoTo ERR_HANDLER
    3. Dim Dlg As Outlook.SelectNamesDialog
    4. m_DialogCaption = "Select Names: *"
    5. m_FindThisName = FindThisName
    6. If hEvent = 0 Then
    7. Dlg = Application.Session.GetSelectNamesDialog
    8. hEvent = SetTimer(0&, 0&, 500, AddressOf TimerProc)
    9. Dlg.Display()
    10. End If
    11. Exit Sub
    12. ERR_HANDLER:
    13. DisableTimer()
    14. End Sub
    15. Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
    16. If uMsg = WM_TIMER Then
    17. DisableTimer()
    18. PrefillSelectNames()
    19. End If
    20. End Sub


    Application.Session.GetSelectNamesDialog wird nicht erkannt. Was ist das richtige Pendant?
    AddressOf TimerProc auch nicht.

    Wie korrekt ansprechen, damit ich weiterkomme?

    LG Jörg
    Hallo Jörg
    Ich hab das ganze einmal in VB.net übersetzt.

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Imports System.Text
    3. Imports System.Threading
    4. Imports Microsoft.Office.Interop
    5. Public Class Form1
    6. Dim objOutlook As Outlook.Application
    7. Dim onNamespace As Outlook.NameSpace
    8. <DllImport("user32.dll", SetLastError:=False)>
    9. Private Shared Function GetDesktopWindow() As IntPtr
    10. End Function
    11. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    12. Private Shared Function GetWindow(ByVal hWnd As IntPtr, ByVal uCmd As UInt32) As IntPtr
    13. End Function
    14. Private Enum GetWindowType As UInteger
    15. GW_HWNDFIRST = 0
    16. GW_HWNDLAST = 1
    17. GW_HWNDNEXT = 2
    18. GW_HWNDPREV = 3
    19. GW_OWNER = 4
    20. GW_CHILD = 5
    21. GW_ENABLEDPOPUP = 6
    22. End Enum
    23. <DllImport("user32.dll", EntryPoint:="GetWindowText")>
    24. Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal
    25. lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
    26. End Function
    27. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    28. Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
    29. End Function
    30. <DllImport("user32.dll", CharSet:=CharSet.Auto)>
    31. Private Shared Function GetClassName(ByVal hWnd As System.IntPtr,
    32. ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As
    33. Integer) As Integer
    34. End Function
    35. <DllImport("user32.dll", CharSet:=CharSet.Auto)>
    36. Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg
    37. As UInteger, ByVal wParam As IntPtr,
    38. <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As
    39. IntPtr
    40. End Function
    41. Private Enum WindowsMessages As UInteger
    42. WM_SETTEXT = &HC
    43. End Enum
    44. Private hEvent As Thread
    45. Private m_FindThisName As String
    46. Private m_DialogCaption As String
    47. Public Sub DisplayDialog(FindThisName As String)
    48. Try
    49. objOutlook =
    50. CType(System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application"),
    51. Outlook.Application)
    52. onNamespace = objOutlook.Session
    53. Dim Dlg As Outlook.SelectNamesDialog
    54. m_DialogCaption = "Namen auswählen: *"
    55. m_FindThisName = FindThisName
    56. If hEvent Is Nothing Then
    57. Dlg = onNamespace.GetSelectNamesDialog
    58. hEvent = New Thread(AddressOf TimerProc)
    59. hEvent.Start()
    60. Dlg.Display()
    61. End If
    62. Catch ex As ArgumentException
    63. End Try
    64. End Sub
    65. Public Sub TimerProc()
    66. Thread.Sleep(500)
    67. PrefillSelectNames()
    68. End Sub
    69. Private Sub PrefillSelectNames()
    70. Dim hnd As IntPtr
    71. Dim OK As Boolean
    72. hnd = GetDesktopWindow()
    73. hnd = FindChildWindowText(hnd, m_DialogCaption)
    74. If hnd.ToInt32 <> 0 Then
    75. hnd = FindChildClassName(hnd, "RichEdit20W")
    76. If hnd.ToInt32 <> 0 Then
    77. SetText(hnd, m_FindThisName)
    78. OK = True
    79. End If
    80. End If
    81. If OK = False Then
    82. MessageBox.Show("Fenster nicht gefunden")
    83. End If
    84. End Sub
    85. Private Function FindChildWindowText(ByVal lHwnd As IntPtr, sFind As String) As IntPtr
    86. Dim lRes As IntPtr
    87. Dim sFindLC As String
    88. lRes = GetWindow(lHwnd, GetWindowType.GW_CHILD)
    89. If lRes.ToInt32 <> 0 Then
    90. sFindLC = sFind.ToLower
    91. Do
    92. If GetText(lRes).ToLower Like sFindLC Then
    93. Return lRes
    94. End If
    95. lRes = GetWindow(lRes, GetWindowType.GW_HWNDNEXT)
    96. Loop While lRes.ToInt32 <> 0
    97. End If
    98. Return IntPtr.Zero
    99. End Function
    100. Public Function GetText(ByVal hWnd As IntPtr) As String
    101. Dim length As Integer
    102. If hWnd.ToInt32 = 0 Then
    103. Return ""
    104. End If
    105. length = GetWindowTextLength(hWnd)
    106. If length = 0 Then
    107. Return ""
    108. End If
    109. Dim sb As New System.Text.StringBuilder("", length)
    110. GetWindowText(hWnd, sb, sb.Capacity + 1)
    111. Return sb.ToString()
    112. End Function
    113. Private Function FindChildClassName(ByVal lHwnd As IntPtr, ByRef sFind As String) As IntPtr
    114. Dim lRes As IntPtr
    115. Dim sFindLC As String
    116. lRes = GetWindow(lHwnd, GetWindowType.GW_CHILD)
    117. If lRes.ToInt32 <> 0 Then
    118. sFindLC = sFind.ToLower
    119. Do
    120. If GetClassName(lRes).ToLower = sFindLC Then
    121. Return lRes
    122. End If
    123. lRes = GetWindow(lRes, GetWindowType.GW_HWNDNEXT)
    124. Loop While lRes.ToInt32 <> 0
    125. End If
    126. Return IntPtr.Zero
    127. End Function
    128. Private Function GetClassName(ByVal lHwnd As IntPtr) As String
    129. Dim sBuffer As New StringBuilder("", 256)
    130. Dim lSize As Integer = GetClassName(lHwnd, sBuffer, sBuffer.Capacity)
    131. If lSize > 0 Then
    132. Return sBuffer.ToString
    133. Else
    134. Return ""
    135. End If
    136. End Function
    137. Private Function SetText(ByVal hwnd As IntPtr, ByVal sText As String) As IntPtr
    138. SetText = SendMessage(hwnd, WindowsMessages.WM_SETTEXT, IntPtr.Zero, sText)
    139. End Function
    140. Private Sub Btn_Test_1_Click(sender As System.Object, e As System.EventArgs) Handles btn_Test_1.Click
    141. DisplayDialog("Jörg")
    142. End Sub
    143. End Class
    Hy Henry,

    joo, das funzt. Danke, bin hellauf begeistert.

    Habe dann noch nach Dlg.Display() folgendes eingebaut, um das Ergebnis weiterbearbeiten zu können:

    VB.NET-Quellcode

    1. 'befinde mich in einer Schleife
    2. If (Dlg.Recipients.Count = 0) Then
    3. myRecipients.Remove(i)
    4. userName = ""
    5. Exit For
    6. Else
    7. myRecipients.Remove(i)
    8. myRecipients.Add(Dlg.Recipients(1).Name)
    9. userName = Dlg.Recipients(1).Name
    10. Exit For
    11. End If