Sub SendDocumentByEmail()
'
'If the user is using Excel 2003 it will send an excel sheet to print and sign
'However, if the user is using 2007 (#12) or greater, it will convert the file to a PDF and send
'
If Val(Application.Version) < 12 Then
Call SendExcelSheetThroughEmail
Else
Call SendTorrentOfEmails
End If
End Sub
Private Sub SendExcelSheetThroughEmail()
'
'Dimensions of email sending program
Dim myEmail As Object
Dim myEmailNS As Object
Dim myEmailMessage As Object
'
'Dimensions to loop through emails and to save new files as .pdf
Application.DisplayAlerts = False
Dim names As Integer, i As Integer, TheFileName As String, TheFileLocation As String
Dim a, b, c As String
names = Application.WorksheetFunction.CountA(Sheets("Ref").Columns(4)) - 2
TheFileLocation = Cells(2, 2) & "\"
TheFileName = Cells(3, 2)
'
'Check Excel Version
'Already been done in the Call Function
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
'
'Repeat for every person
'Transform the email file to a .xls
For i = 0 To names
'
Set myEmail = CreateObject("Outlook.Application")
Set myEmailNS = myEmail.GetNamespace("MAPI")
Set myEmailMessage = myEmail.CreateItem(0)
Sheets("Ref").Cells(14, 2) = i
a = Sheets("Ref").Cells(16, 4).Offset(Cells(14, 2), 0)
b = Sheets("Ref").Cells(4, 2)
c = Sheets("Ref").Cells(5, 2)
If a = "" Then Exit Sub
Sheets("Email").Copy
'''''''''''''''''''''''''''''
Cells.Copy
Cells(1, 1).Select
ActiveCell.PasteSpecial Paste:=xlPasteValues
'''''''''''''''''''''''''''''
'Save copy as xls now
ActiveWorkbook.SaveAs Filename:= _
TheFileLocation & TheFileName & FileExtStr, FileFormat:=FileFormatNum
ActiveWindow.Close
'
'Construct the email
myEmailMessage.To = a
myEmailMessage.Subject = b
myEmailMessage.Body = c
myEmailMessage.Attachments.Add TheFileLocation & TheFileName & FileExtStr 'adds attachment to email
myEmailMessage.Send
'
Set myEmail = Nothing
Set myEmailNS = Nothing
Set myEmailMessage = Nothing
Next i
End Sub
Private Sub SendTorrentOfEmails()
'
'Dimensions of email sending program
Dim myEmail As Object
Dim myEmailNS As Object
Dim myEmailMessage As Object
'
'Dimensions to loop through emails and to save new files as .pdf
Application.DisplayAlerts = False
Dim names As Integer, i As Integer, TheFileName As String, TheFileLocation As String
Dim a, b, c As String
names = Application.WorksheetFunction.CountA(Sheets("Ref").Columns(4)) - 2
TheFileLocation = Cells(2, 2) & "\"
TheFileName = Cells(3, 2)
'
'
'Repeat for every person
'Transform the email file to a .pdf
For i = 0 To names
'
Set myEmail = CreateObject("Outlook.Application")
Set myEmailNS = myEmail.GetNamespace("MAPI")
Set myEmailMessage = myEmail.CreateItem(0)
Sheets("Ref").Cells(14, 2) = i
a = Sheets("Ref").Cells(16, 4).Offset(Cells(14, 2), 0)
b = Sheets("Ref").Cells(4, 2)
c = Sheets("Ref").Cells(5, 2)
If a = "" Then Exit Sub
Sheets("Email").Copy
'''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''
'Save copy as PDF now as you are using Excel 2007-2010
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
TheFileLocation & TheFileName & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
ActiveWindow.Close
'
'Construct the email
myEmailMessage.To = a
myEmailMessage.Subject = b
myEmailMessage.Body = c
myEmailMessage.Attachments.Add TheFileLocation & TheFileName & ".pdf" 'adds attachment to email
myEmailMessage.Send
'
Set myEmail = Nothing
Set myEmailNS = Nothing
Set myEmailMessage = Nothing
Next i
End Sub