Mail every worksheet with Different signature use excel vba - excel

i need your help
the below code work to send email for the sheets my question ?
how i can change the signature automated ? i have the name of the signature in the excel file lets call it (b2) .
its possible to make it ?
Note : i use excel 365 and widows 10
Sub Mail_Every_Worksheet()
Dim sh As Worksheet
Dim wb As Workbook
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
TempFilePath = Environ$("temp") & "\"
'You use Excel 2007-2016
FileExtStr = ".xls": FileFormatNum = 52
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set OutApp = CreateObject("Outlook.Application")
For Each sh In ThisWorkbook.Worksheets
If sh.Range("A2").Value Like "?*#?*.?*" Then
sh.Copy
Set wb = ActiveWorkbook
TempFileName = sh.Name
Set OutMail = OutApp.CreateItem(0)
With wb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.Attachments.Add wb.FullName
.Display
strbody = "HI sony "
.to = sh.Range("A2").Value
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = "HI sony " & "<br>" & .HTMLBody
.Send
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
End With
On Error GoTo 0
.Close savechanges:=False
End With
Set OutMail = Nothing
Kill TempFilePath & TempFileName & FileExtStr
End If
Next sh
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

If you just want to add the default signature then display the email before sending it:
.Display
.HTMLBody = strbody & "<br>" & .HTMLBody
.Send
If, however, you want to use a specific signature file then you will need to read that file:
SigString = Environ("appdata") & "\Microsoft\Signatures\B2.htm"
If Dir(SigString) = "" Then
OutSignature = ""
Else
Dim fso As Object
Dim sf As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set sf = fso.GetFile(SigString).OpenAsTextStream(1, -2)
OutSignature = sf.readall
sf.Close
End If
.HTMLBody = strbody & "<br>" & OutSignature
.Send
I use Excel 2013 though this answer should not be restricted to that version.

Related

How to send one email to list of email addresses in Excel workbook?

I want to send to a list of email addresses in my workbook.
How would I go about that with what I have for the mailing section of my code?
I want to have column R named mailing list and it will send to whatever email addresses are inserted into that column/list all together.
Sub SendReminderMail1()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb1 = ActiveWorkbook
TempFilePath = Environ$("temp") & "\"
TempFileName = "Copy of " & wb1.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
wb2.Worksheets(1).Range("A1").Value = "Copy created on " & Format(Date, "dd-mmm-yyyy")
wb2.Save
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = " "
.CC = ""
.BCC = ""
.Subject = "Rotations needed for ."
.Body = "Hey there, equipment needs to be rotated."
.Attachments.Add wb2.FullName
.Display 'or use .Send to send with display proof reading
End With
On Error GoTo 0
wb2.Close savechanges:=False
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your Automated Email for BP Rotations was successfully ran at " & TimeValue(Now), vbInformation
End Sub
In your code I've you set the recipients fields to empty strings:
With OutMail
.To = " "
.CC = ""
.BCC = ""
Instead, you need to read values from the column R and add recipients for the email. To add recipients I'd recommend using the Recipients collection which can be retrieved using the corresponding property of the MailItem class.
Dim recipients As Outlook.Recipients = Nothing
Set recipients = mail.Recipients
' now we add new recipietns to the e-mail
recipientTo = recipients.Add("Eugene")
recipientTo.Type = Outlook.OlMailRecipientType.olTo
recipientCC = recipients.Add("Dmitry")
recipientCC.Type = Outlook.OlMailRecipientType.olCC
recipientBCC = recipients.Add("eugene.astafiev#somedomain.com")
recipientBCC.Type = Outlook.OlMailRecipientType.olBCC
recipients.ResolveAll()
Read more about that in the How To: Fill TO,CC and BCC fields in Outlook programmatically article.
Mail Merge not your cup of tea huh...
Maybe what you need is a Do while Loop where it references a cell in a Table of people, then moves down each step till the E-mail is blank just chugging through Row after Row of E-mails driving that sweet CPU usage up.
Like a user programmed Mail Merge but in Excel and not in a Word Processor... Like Mail Merge... In Word, but not in Word, In Excel In VBA...

Compiling condition across multiple worksheets to output 1 "solution". Also call out which worksheets(s) fit the condition

I am new to vba and struggling with trying to accomplish having a single overall email send out if any worksheet column L/M have a value below 1 and want it to call out which sheet (or sheets) had the value <1 in the email body or subject line. I have spent countless hours searching the web, but nothing so far has really worked for me. The MsgBox function is working fine, just having issues with compiling the results to 1 email naming which worksheet had the <1 value and 1 "solution" for the whole workbook, instead of having an email sent for every single worksheet that the conditions fit. Thank you in advance.
Option Explicit
Sub Main()
Dim sh As Worksheet, i As Long
For Each sh In ActiveWorkbook.Worksheets
With WorksheetFunction
If .CountIf(sh.Range("L3:L26"), "<1") > 0 Then
Call SendReminderMail1
Else
MsgBox "Rotations NOT needed for """ & sh.Name & """."
End If
If .CountIf(sh.Range("M3:M20"), "<1") > 0 Then
Call SendReminderMail2
Else
MsgBox "Functions are NOT needed for """ & sh.Name & """."
End If
End With
Next
End Sub
Sub SendReminderMail1()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim sh As Worksheet
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb1 = ActiveWorkbook
TempFilePath = Environ$("temp") & "\"
TempFileName = "Copy of " & wb1.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
wb2.Worksheets(1).Range("A1").Value = "Copy created on " & Format(Date, "dd-mmm-yyyy")
wb2.Save
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "test#test.com"
.CC = ""
.BCC = ""
.Subject = "Rotations are due for """ & sh.Name & """."
.Body = "Hi there bud, ya need to take a good ole look at this here document. You've been slackin', let's fix that."
.Attachments.Add wb2.FullName
.Display 'or use .Display
End With
On Error GoTo 0
wb2.Close savechanges:=False
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your Automated Email for Rotations was successfully ran at " & TimeValue(Now), vbInformation
End Sub
Sub SendReminderMail2()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim sh As Worksheet
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb1 = ActiveWorkbook
TempFilePath = Environ$("temp") & "\"
TempFileName = "Copy of " & wb1.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
wb2.Worksheets(1).Range("A1").Value = "Copy created on " & Format(Date, "dd-mmm-yyyy")
wb2.Save
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "test#test.com"
.CC = ""
.BCC = ""
.Subject = "Functions are due for """ & sh.Name & """."""
.Body = "Hi there bud, ya need to take a good ole look at this here document."
.Attachments.Add wb2.FullName
.Display 'or use .Display
End With
On Error GoTo 0
wb2.Close savechanges:=False
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your Automated Email for Functions was successfully ran at " & TimeValue(Now), vbInformation
End Sub
Try something like this:
Sub Main()
Dim sh As Worksheet, i As Long, shtsRotations As String
Dim shtsFunctions As String, shtsOK As String
For Each sh In ActiveWorkbook.Worksheets
If Application.CountIf(sh.Range("L3:L26"), "<1") > 0 Then
shtsRotations = shtsRotations & vbLf & sh.Name
Else
shtsOK = shtsOK & vbLf & sh.Name & " (Rotations)"
End If
If Application.CountIf(sh.Range("M3:M20"), "<1") > 0 Then
shtsFunctions = shtsFunctions & vbLf & sh.Name
Else
shtsOK = shtsOK & vbLf & sh.Name & " (Functions)"
End If
Next sh
If Len(shtsRotations) > 0 Then
SendReminderMail "test#test.com", "Rotations are due for worksheet(s).", _
"Hi there bud, ya need to take a good ole look at this here document." & _
"Check sheets: " & shtsRotations & vbLf & _
"You've been slackin', let's fix that."
End If
If Len(shtsFunctions) > 0 Then
SendReminderMail "test#test.com", "Functions are due for worksheet(s).", _
"Hi there bud, ya need to take a good ole look at this here document." & _
"Check sheets: " & shtsFunctions & vbLf & _
"You've been slackin', let's fix that."
End If
If Len(shtsOK) > 0 Then
MsgBox "These sheets are OK: " & vbLf & shtsOK, vbInformation
End If
End Sub
Sub SendReminderMail(sTo As String, sSubject As String, sBody As String)
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim sh As Worksheet
Dim TempFilePath As String, TempFileName As String
Dim FileExtStr As String, OutApp As Object, OutMail As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb1 = ActiveWorkbook
TempFilePath = Environ$("temp") & "\"
TempFileName = "Copy of " & wb1.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
wb2.Worksheets(1).Range("A1").Value = "Copy created on " & Format(Date, "dd-mmm-yyyy")
wb2.Save
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "test#test.com"
.CC = ""
.BCC = ""
.Subject = sSubject
.Body = sBody
.Attachments.Add wb2.FullName
.Display
End With
On Error GoTo 0
wb2.Close savechanges:=False
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your Automated Email was successfully ran at " & TimeValue(Now), vbInformation
End Sub
You can reduce your code size by creating a single sub to send a mail, and passing in the variable parts.

.Send is for my below vba code seems to not work

The below code works fine just that .Send is having issues i dont know why
Sub Email_CurrentWorkBook()
Dim UserInputToEmail As String
'Do not forget to change the email ID before running this code
Dim OlApp As Object
Dim NewMail As Object
Dim TempFilePath As String
Dim FileExt As String
Dim TempFileName As String
Dim FileFullPath As String
Dim MyWb As Workbook
Set MyWb = ThisWorkbook
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
TempFilePath = Environ$("temp") & "\"
FileExt = "." & LCase(Right(MyWb.Name, Len(MyWb.Name) - InStrRev(MyWb.Name, ".", , 1)))
TempFileName = MyWb.Name & "-" & Format(Now, "dd-mmm-yy h-mm-ss")
FileFullPath = TempFilePath & TempFileName & FileExt
MyWb.SaveCopyAs FileFullPath
Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)
UserInputToEmail = Application.InputBox("Please enter an email address: ")
With NewMail
.To = UserInputToEmail
.CC = ""
.BCC = ""
.Subject = "Automated file output"
.Body = "Hello, Please find the attached your automated script output"
.Attachments.Add FileFullPath '
--- full path of the temp file where it is saved
Dim T1 As Variant
Dim T2 As Variant
T1 = Now()
T2 = DateAdd("s", 1, T1)
Do Until T2 <= T1
T1 = Now()
Loop
NewMail.Send
.Send
End With
On Error GoTo 0
Kill FileFullPath
Set NewMail = Nothing
Set OlApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

How can I get an Excel generated email to auto fill the subject with a cell value?

I am trying to get Excel to auto fill the subject line with a specific cell value when an email is generated.
Currently My code for the email process is:
Sub EmailStage1Answers()
'
' EmailStage1Answers Macro
Dim wb1 As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb1 = ActiveWorkbook
TempFilePath = Environ$("temp") & "\"
TempFileName = "Copy of " & wb1.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "EMAIL HIDDEN FOR POSTING"
.CC = ""
.BCC = ""
.Subject = "Assessment Form, Stage 1 ***INSERT PROJECT NAME***"
.Body = "Please see the latest copy of the Assessment Form attached. NOTE: Please ensure you have inserted the project name above."
.Attachments.Add TempFilePath & TempFileName & FileExtStr
.display
End With
On Error GoTo 0
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
The workbook has a total of 19 pages and I want to populate the subject from a specific cell on the third page.
Can anyone advise?

Mail sheets to address in cell A1 - keeping format

Please can someone advise me on this issue. I have the below code from Ron de Bruin site to send multiple sheets to email addresses in cell A1.
However when the email is received it has changed the format of the times on the sheets i.e 16:00:00 changed to 0.666666667
can anyone see how it can be adapted to keep the 16:00:00?
Option Explicit
Sub Mail_Every_Worksheet()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim sh As Worksheet
Dim wb As Workbook
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
TempFilePath = Environ$("temp") & ""
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2016
FileExtStr = ".xlsm": FileFormatNum = 52
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set OutApp = CreateObject("Outlook.Application")
For Each sh In ThisWorkbook.Worksheets
If sh.Range("A1").Value Like "?*#?*.?*" Then
sh.Copy
Set wb = ActiveWorkbook
'Change all cells in the worksheet to values
With wb.Sheets(1).UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
End With
Application.CutCopyMode = False
TempFileName = "Sheet " & sh.Name & " of " _
& ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutMail = OutApp.CreateItem(0)
With wb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = sh.Range("A1").Value
.CC = ""
.BCC = ""
.Subject = "TEST"
.Body = "Hi there"
.Attachments.Add wb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0
.Close savechanges:=False
End With
Set OutMail = Nothing
Kill TempFilePath & TempFileName & FileExtStr
End If
Next sh
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
I got nerd sniped by your question and refactored your code.
Public Sub Mail_Every_Worksheet()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
Dim emailAddress As String
emailAddress = sh.Range("A1").Value2
If IsValidEmailAddress(emailAddress) Then
Dim tempFileName As String
tempFileName = "Sheet " & sh.Name & " of " & ThisWorkbook.Name & " " & Format$(Now, "dd-mmm-yy h-mm-ss")
Dim tempBook As Workbook
Set tempBook = CreateTempWorkbookFrom(sh, Environ$("temp"), tempFileName)
Dim tempBookFullPath As String
tempBookFullPath = tempBook.FullName
tempBook.Close
SendOutlookEmailTo emailAddress, vbNullString, vbNullString, "Subject", "Body", tempBookFullPath
Kill tempBookFullPath
End If
Next
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Private Function IsValidEmailAddress(ByVal value As String) As Boolean
IsValidEmailAddress = (value Like "?*#?*.?*")
End Function
Private Function CreateTempWorkbookFrom(ByVal copySheet As Worksheet, ByVal tempSavePath As String, ByVal tempFileName As String) As Workbook
If Right$(tempSavePath, 1) <> Application.PathSeparator Then
tempSavePath = tempSavePath & Application.PathSeparator
End If
copySheet.Copy
Set CreateTempWorkbookFrom = ActiveWorkbook
With CreateTempWorkbookFrom.Worksheets(1).UsedRange
'Change all cells in the worksheet to values
.Cells.Value2 = .Cells.Value2
End With
If Val(Application.Version) < 12 Then
CreateTempWorkbookFrom.SaveAs tempSavePath & tempFileName & ".xls", xlWorkbookNormal
Else
CreateTempWorkbookFrom.SaveAs tempSavePath & tempFileName & ".xlsm", xlOpenXMLWorkbookMacroEnabled
End If
End Function
Private Sub SendOutlookEmailTo(ByVal emailAddress As String, _
ByVal CC As String, _
ByVal BCC As String, _
ByVal Subject As String, _
ByVal Body As String, _
ParamArray attachments() As Variant)
On Error Resume Next
Dim mailItem As Object 'Outlook.mailItem 'Tools>References>Microsoft Outlook X.xx Object Library
Const OutlookMailItem As Long = 0
Set mailItem = CreateObject("Outlook.Application").CreateItem(OutlookMailItem) ' Outlook.Application.CreateItem(olMailItem)
With mailItem
.To = emailAddress
.CC = CC
.BCC = BCC
.Subject = Subject
.Body = Body
Dim attachment As Variant
For Each attachment In attachments
.attachments.Add attachment
Next
.Display
.Send
End With
On Error GoTo 0
End Sub

Resources