Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-21-2024, 03:51 AM
ndandge ndandge is offline Word VBA code to retrieve email address from Outlook Windows 7 64bit Word VBA code to retrieve email address from Outlook Office 2007
Novice
Word VBA code to retrieve email address from Outlook
 
Join Date: Nov 2015
Posts: 9
ndandge is on a distinguished road
Default Word VBA code to retrieve email address from Outlook

I am new VBA code user. I am using Microsoft office 365 and I am trying to create Word VBA code which will search names present in the word document in outlook contacts and retrieve email addresses in below format. Surname, Name ;

But instead of email address, I am receiving below text. How can I get actual email address (e.g. nameSurname@gmail.com) instead of this text?

/o=ExchangeLabs/ou=Exchange Administrative Group....

I have created below word vba code.

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
Reply With Quote
Reply

Tags
outlook 2011, word vba code



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error when sending email from Excel using an Outlook.account identified by its email address. MikeK Excel Programming 0 06-20-2023 06:26 AM
outlook app of android changes outgoing email address to a gmail address bruegel Outlook 0 01-05-2023 08:13 AM
Word VBA code to retrieve email address from Outlook Outlook won't reply/forward email from one email address rluna68 Outlook 2 01-07-2013 12:42 PM
Word VBA code to retrieve email address from Outlook Outlook obtaining Email Address from Word Atomizer Outlook 1 05-11-2011 08:17 AM
Outlook Does Not Retrieve Email from Newest agusbagus Outlook 0 11-07-2010 07:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft