• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Automatic Leave application notication to manager when employee add their username in excel calender

SYEDZAHED

New Member
Hi All,
I am trying to write a macro in which I want automatic outlook notification mail when my associates write there user name in the excel calendar date cell. As soon as they write there user name, I should get outlook notification mail that xxx person applied leave for date xx-xx-xx. I am trying this since last three days and I am tired of trying this. Can anyone help me writing this macro and I have to submit to my supervisors also ASAP. Please help me in this regard. I am attaching a excel calendar, please look into this.

Thanks
Syed Zahed Ahmed
 

Attachments

  • Excel-2018-Calendar.xlsm
    31.1 KB · Views: 14
>> Use Code -tags <<
Code:
Sub Mail_Selection_Range_Outlook_Body()

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object

Set rng = Nothing
' Only send the visible cells in the selection.
Sheets("October 2018").Unprotect Password:="1234"

Set rng = Sheets("October 2018").Range("B4:m35").SpecialCells(xlCellTypeVisible)

If rng Is Nothing Then
  MsgBox "The selection is not a range or the sheet is protected. " & _
  vbNewLine & "Please correct and try again.", vbOKOnly
  Exit Sub
End If

With Application
  .EnableEvents = False
  .ScreenUpdating = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


With OutMail
  .To = "syed.zahed@optum.com"
  .CC = ""
  .BCC = ""
  .Subject = "One of your associte raise a request for leave"
  .HTMLBody = RangetoHTML(rng)
  ' In place of the following statement, you can use ".Display" to
  ' display the e-mail message.
  .display
End With
On Error GoTo 0

With Application
  .EnableEvents = True
  .ScreenUpdating = True
End With

Set OutMail = Nothing
Set OutApp = Nothing
ActiveWorkbook.Close savechanges:=False
End Sub


Function RangetoHTML(rng As Range)
' By Ron de Bruin.
  Dim fso As Object
  Dim ts As Object
  Dim TempFile As String
  Dim TempWB As Workbook

  TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

  'Copy the range and create a new workbook to past the data in
  rng.Copy
  Set TempWB = Workbooks.Add(1)
  With TempWB.Sheets(1)
  .Cells(1).PasteSpecial Paste:=8
  .Cells(1).PasteSpecial xlPasteValues, , False, False
  .Cells(1).PasteSpecial xlPasteFormats, , False, False
  .Cells(1).Select
  Application.CutCopyMode = False
  On Error Resume Next
  .DrawingObjects.Visible = True
  .DrawingObjects.Delete
  On Error GoTo 0
  End With

  'Publish the sheet to a htm file
  With TempWB.PublishObjects.Add( _
  SourceType:=xlSourceRange, _
  Filename:=TempFile, _
  Sheet:=TempWB.Sheets(1).Name, _
  Source:=TempWB.Sheets(1).UsedRange.Address, _
  HtmlType:=xlHtmlStatic)
  .Publish (True)
  End With

  'Read all data from the htm file into RangetoHTML
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
  RangetoHTML = ts.ReadAll
  ts.Close
  RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
  "align=left x:publishsource=")

  'Close TempWB
  TempWB.Close savechanges:=False

  'Delete the htm file we used in this function
  Kill TempFile

  Set ts = Nothing
  Set fso = Nothing
  Set TempWB = Nothing
End Function
 
With the above code I am able to send the screen shot of the excel, but I don't want screen shot in mail. I want to display just name of the employee in body with some matter like "XXX associate applied leave on this date XX-XX-XX and it should be automatic without running macro manually.
Thanks
 
Back
Top