applying username within text

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Applying a Username within Text on an outlook Email

Applying a Username within Text on an outlook Email

resolvedResolved · Urgent Priority · Version 2016

Sean has attended:
Excel VBA Introduction course
Excel VBA Intermediate course

Applying a Username within Text on an outlook Email

Hi there,

I currently have created the following code

Sub SendEmail()

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

Set OutApp = CreateObject("Outlook.application")
Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail

.To = "sean.hickman@idealboilers.com"
.CC = ""
.BCC = ""
.Subject = "ERP LABEL SHEET SAVED IN NEW LOCATION"
.Body = " <INSERT USERNAME> has saved the Master Data Sheet in a new Location"

'walkout from the loading bar
.Display
Application.SendKeys "%s"

'.Send
End With

End Sub

I am trying to find a way of taking the username of the person with the sheet currently open and inserting it into the email being sent to the owner of the sheet.

The idea is that this code will enter into a userform at the 'save' function questioning if the person has the authority to save this file elsewhere. If they click yes this code will run. Therefore controlling all edits to the document.

(In an ideal world I'd like to include the new file save location in the email as well, though I'm not sure if this is beyond my Excel VBA Intermediate knowledge!)

any help with this?

Sean Hickman

RE: Applying a Username within Text on an outlook Email

Hi Sean,

Thank you for the forum question.


Try:

Sub SendEmail()

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

Dim strUsername as String

strUsername=Application.UserName

Set OutApp = CreateObject("Outlook.application")
Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail

.To = "sean.hickman@idealboilers.com"
.CC = ""
.BCC = ""
.Subject = "ERP LABEL SHEET SAVED IN NEW LOCATION"
.Body = strUsername & " has saved the Master Data Sheet in a new Location"

'walkout from the loading bar
.Display
Application.SendKeys "%s"

'.Send
End With

End Sub




Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Applying a Username within Text on an outlook Email

Thanks Jens,

I tried using application.username as a str before but I put it inside the Text. Using it outside and with a & has worked just fine!

Thank you very much!

RE: Applying a Username within Text on an outlook Email

Hi Sean,

I guess you found the solution yourself.

Just remove:

.Send

The code otherwise will try to send the email twice.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Applying a Username within Text on an outlook Email

Hi Jens,

I've inserted my Code into a Userform cmd and the walkout from the loading bar element is not working, I'm still getting the loading bar which I don't want as it gives the user the opportunity to cancel the command and save the file anyway!

Private Sub cmdContinue_Click()

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

Dim strUsername As String

strUsername = Application.UserName

Set OutApp = CreateObject("Outlook.application")
Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail

.To = "sean.hickman@idealboilers.com"
.CC = ""
.BCC = ""
.Subject = "ERP LABEL SHEET SAVED IN NEW LOCATION"
.Body = strUsername & " has saved the Master Data Sheet in a new Location"

'walkout from the loading bar
.Display
Application.SendKeys "%s"

.Send
End With

End Sub

can you help?

all the best.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Applying and removing border from cell in Excel 2010

Did you know the shortcut key for applying and removing the outline border for a cell?

CTRL+SHIFT+& Applies the outline border to the selected cells.
CTRL+SHIFT_ Removes the outline border from the selected cells.

View all Excel hints and tips


Server loaded in 0.05 secs.