Outlook VBA to open Excel attachment and send recipient's email address to a workbook cell?

Not open for further replies.

Dan Smith

Outlook version
Outlook 2007
Email Account
Exchange Server 2007
Hello All,

Using VBA with Outlook 2007, is it possible to open an Excel attachment from an unsent new or reply email message, pass the recipient's email address from the "To:" field to a cell in the workbook that was just opened and then close the email message?

After several months of trying to send cell data from an attached Excel 2007 workbook to the open but unsent Outlook 2007 new or reply message, I realized I may be looking at the task from the wrong direction.

Any help would be sincerely appreciates, thanks!

Forum Admin

Senior Member
It would be possible. This will save the attachment from the open message and open it - you'll need to reference Excel and grab the value from the desired cell.

Dim oItem As Outlook.MailItem
Dim strfile As String
Dim sText As String

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
  "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
  ByVal lpFile As String, ByVal lpParameters As String, _
  ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub OpenExcel()
    Dim oApp As Object, oNS As Object, oInbox As Object
    Dim oAttach As Object
    Dim FileName As String
    Dim enviro As String
    enviro = CStr(Environ("USERPROFILE"))
    FileName = enviro & "\Documents\"

    Set oApp = Outlook.Application
    Set oItem = oApp.ActiveInspector.CurrentItem

       If oItem.Attachments.count <> 0 Then
            For Each oAttach In oItem.Attachments
            strfile = FileName & oAttach.FileName
                oAttach.SaveAsFile strfile
                Exit For
            MsgBox "The message doesn't have an attachment"
        End If
 ShellExecute 0, "open", strfile, vbNullString, vbNullString, 0
 oItem.To = sText
 End Sub

Sub CopyFromExcel()
 Dim xlApp As Object
 Dim xlWB As Object
 Dim xlSheet As Object
     On Error Resume Next
     Set xlApp = GetObject(, "Excel.Application")
     If Err <> 0 Then
         Application.StatusBar = "Please wait while Excel source is opened ... "
         Set xlApp = CreateObject("Excel.Application")
         bXStarted = True
     End If
     On Error GoTo 0
     'Open the workbook to input the data
     Set xlWB = xlApp.Workbooks.Open(strfile)
     Set xlSheet = xlWB.Sheets("Sheet1")

     sText = xlSheet.Range("A1")
     xlWB.Close 1
     If bXStarted Then
     End If
     Set xlApp = Nothing
     Set xlWB = Nothing
     Set xlSheet = Nothing
 End Sub

Dan Smith

Outlook version
Outlook 2007
Email Account
Exchange Server 2007
Thank you for your response and I sincerely appreciate you taking your time to respond to my question. Your code works perfectly for inputting cell data from the newly opened Excel document into the "To:" address filed of a new message but I am actually looking to do the reverse. Is there a way to send the recipients email address from the "To:" field to a cell in the newly opened Excel attachment? Thank you for your help!
Not open for further replies.