Я использую Microsoft Office 365.
Я пытаюсь выполнить поиск контактов Outlook по именам в документе Word, чтобы получить адреса электронной почты в формате ниже.
Surname, Name <email address>;
Я получаю:
/o=ExchangeLabs/ou=Exchange Administrative Group....
Как я могу получить адрес электронной почты в формате «имяфамилия@gmail.com»?
Word-код VBA.
Option Explicit
Sub SendEmail()
Dim Names As String
Dim Doc As Word.Document
Dim rng As Word.Range
Set Doc = ActiveDocument
Names = Selection.Text
Selection.Collapse Direction:=wdCollapseEnd
Selection.Move Unit:=wdStory, Count:=1
Selection.Text = vbNewLine
Dim OL As Outlook.Application
Dim EmailItem As Outlook.MailItem
Dim Rec As Outlook.Recipient
' Check if Outlook is already open
On Error Resume Next
Set OL = GetObject(, "Outlook.Application")
On Error GoTo 0
' If Outlook is not open, create a new instance
If OL Is Nothing Then
Set OL = New Outlook.Application
End If
Set EmailItem = OL.CreateItem(olMailItem)
With EmailItem
.Display
.CC = Names
' Ensure names are properly formatted
Dim RecipientsResolved As Boolean
RecipientsResolved = .Recipients.ResolveAll
If Not RecipientsResolved Then
MsgBox "One or more recipients could not be resolved. Please check the names and try again.", vbExclamation
End If
For Each Rec In .Recipients
Selection.Collapse Direction:=wdCollapseEnd
Selection.Text = Rec.Name & " <" & Rec.Address & ">; "
Selection.Collapse Direction:=wdCollapseEnd
Next Rec
End With
Set OL = Nothing
Set EmailItem = Nothing
End Sub
Это совершенно действительный адрес электронной почты типа "EX"
, пока вы ожидаете "SMTP"
. Используйте Recipient.AddressEntry
, чтобы получить объект AddressEntry
, а затем проверьте AddressEntry.Type
. Если это "SMTP"
, просто используйте AddressEntry.Address
. В противном случае вызовите AddressEntry.GetExchangeUser()
(проверьте наличие нуля) и используйте ExchangeUser.PrimarySmtpAddress
.
Отвечает ли это на ваш вопрос? Как извлечь адреса электронной почты из поля «Кому» в Outlook?