VBA to extract users name from outlook

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
569
Office Version
  1. 365
Platform
  1. Windows
I have a macro that will transfer the data from the current worksheet to a transfer worksheet, open an email, attach the transfer worksheet, enter the email address on the to and CC lines, enter a subject and a body message. I would like to extract the first and last name as well as the users email, to add to the body of the email message. I have included my code below. I don't know how to get the information from outlook for the users name and email address. Once I have the users name, I would like to be able to parse out the first and last names.


VBA Code:
Sub EMAIL_CORRECTIONS()

    Dim Source As Range
    Dim Dest As Workbook
    Dim wb As Workbook
    Dim empFilePath As String
    Dim TempFileName As String
    Dim ToAdd As String
    Dim CCAdd As String
    Dim EM_Body As String
            
    Dim LastRow As Long
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Lrow As Long
    
'   Define the list of email addresses to send list to.
    ToAdd = Sheets("Intro Page").Range("AA26").Value
'    CCAdd = Sheets("Intro Page").Range("AA27").Value

'   Sheets("Corrections").Unprotect

'   Define last row of data
    Sheets("Corrections").Activate
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row

    Sheets("Corrections").Range("A6:C" & LastRow).Copy

'   Paste a copy of the list on the transfer page
    Sheets("Transfer Page").Visible = True
    Sheets("Transfer Page").Range("A2").PasteSpecial (xlPasteValues)

'   Define last row of data on the Transfer page
    Sheets("Transfer Page").Select
    Lrow = Cells(Rows.Count, 1).End(xlUp).Row


'   Define the block of data to create a sheet from.
    Set Source = Nothing
    On Error Resume Next
    Set Source = Sheets("Transfer Page").Range("A1:C" & Lrow).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    Set wb = ActiveWorkbook
    Set Dest = Workbooks.Add(xlWBATWorksheet)
    Source.Copy
    With Dest.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
    End With
    
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Data Corrections" & " " & Format(Now, "dd-mmm-yyyy")
                
    FileExtStr = ".xlsx": FileFormatNum = 51

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    With Dest
        .SaveAs TempFilePath & TempFileName & FileExtStr, _
                FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .To = ToAdd
'            .CC = CCAdd
'            .BCC = ""
            .Subject = "Data Corrections for " & Sheets("Intro Page").Range("C4") & " - Please Review"
            .HTMLBody = "<html><body lang=EN-US link='#0563C1' vlink='#954F72'>" & _
                "<span style='font-size:12.5pt'>Greetings,<o:p><br><br>" & _
                "<span style='font-size:12.5pt'>The attached list of Part Numbers are not found.<o:p>" & _
                "</p><span style='font-size:12.5pt'>Please review the list and initiate corrections/additions to the data table." & _
                "</body></html>" '& GetEmailSig
            .BodyFormat = 2 'html format
            .Attachments.Add Dest.FullName
            '.Send
            .Display
        End With
        On Error GoTo 0
        .Close SaveChanges:=False
    End With
    
    Kill TempFilePath & TempFileName & FileExtStr
    
    Set OutMail = Nothing
    Set OutApp = Nothing

'   Clear Data from transfer page
    Sheets("Transfer Page").Activate
    Sheets("Transfer Page").Range("A2:C" & Lrow).EntireRow.Delete
    Sheets("Transfer Page").Range("A2").Select
    Sheets("Transfer Page").Visible = False
    Sheets("Mechanical table").Select

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Do you mean the information about the current user or someone from address book?
 
Upvote 0

Forum statistics

Threads
1,215,191
Messages
6,123,553
Members
449,108
Latest member
rache47

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top