Sub Mail ()
'Working in 2000-2010
Dim strbody As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim cat As String
Dim subcat As String
Dim TempFileNameJL As String
Dim dat1 As String
Dim Weeknr As Integer
Dim OutApp As Outlook.Application ' in extra van VBA > verwijzing > microsoft outlook office library (aanvinken)
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
Weeknr = 1 + Int((Date - DateSerial(Year(Date + 4 - Weekday(Date + 6)), 1, 5) + Weekday(DateSerial(Year(Date + 4 - Weekday(Date + 6)), 1, 3))) / 7)
dat1 = Date + 1
week1 = Weeknr
Range("B2").Select
cat = Range("B2")
Range("C2").Select
subcat = Range("C2")
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the sheet to a new workbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 2000-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2010, we exit the sub when your answer is
'NO in the security dialog that you only see when you copy
'an sheet from a xlsm file with macro's disabled.
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is NO in the security dialog"
Exit Sub
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With
'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = "TEST IN ONDERWERP - " & Sourcewb.Name & " " _
& Format(Now, "dd-mmm-yy")
TempFileNameJL = Sourcewb.Name
TempFileNameJL = Mid(TempFileName, 34, 6)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, _
FileFormat:=FileFormatNum
On Error Resume Next
If ActiveWorkbook.Path <> "" Then
strbody = "<font size=""3"" face=""Arial Narrow"">" & _
"Tekstlijn,<br>" & _
"Tekstlijn,<br><br>" & _
"Tekstlijn : " & "<font size=""3"" face=""Arial Black"" color=""blue"">" & Weeknr & "<br><br>" & _
"<font size=""3"" face=""Arial Narrow"" color=""black"">" & _
"Tekstlijn: " & "<font size=""3"" face=""Arial Black"" color=""blue"" FontStyle=""bold"">" & cat & "<br>" & _
"<font size=""3"" face=""Arial Narrow"" color=""black"">" & _
"Tekstlijn : " & "<font size=""3"" face=""Arial Black"" color=""blue"" FontStyle=""bold"">" & subcat & "<br>" & _
"<font size=""3"" face=""Arial Narrow"" color=""black"">" & _
"Tekstlijn : " & "<font size=""3"" face=""Arial Black"" color=""green"" FontStyle=""bold"">" & TempFileNameJL & "<font size=""3"" face=""Constantia"" color=""red"" FontStyle=""bold"">" & " Tekstlijn " & "<br><br>" & _
"<font size=""3"" face=""Arial Narrow"" color=""black"">" & _
"Tekstlijn " & "<font size=""3"" face=""Arial Black"" color=""red"" FontStyle=""bold"">" & dat1 & "Tekstlijn" & "<br><br>" & _
"<font size=""3"" face=""Arial Narrow"" color=""black"">" & _
"Tekstlijn," & "<br>" & _
"Tekstlijn" & "<br><br>" & _
"<font size=""3"" face=""Arial Narrow"" color=""black"">" & _
"Tekstlijn" & "<br>" & _
"Tekstlijn" & "<br>" & _
"Tekstlijn" & "<br><br><br>" & _
""
On Error Resume Next
With OutMail
.To = "" ' email invullen indien gewenst
.CC = ""
.Attachments.Add Destwb.FullName
.BCC = "test " ' groep emailadressen aangemaakt in outlook
.Subject = "Tekstlijn" & Weeknr & "/" & TempFileNameJL & " -" & " Tekstlijn: " & Weeknr & "/" & TempFileNameJL
.htmlBody = strbody
.SendUsingAccount = OutApp.Session.Accounts.Item(5) ' het cijfer bepaalt de account in outlook waarvan men wil versturen
.Display 'or use .Send
End With
On Error GoTo 0
.Close SaveChanges:=False
Set OutMail = Nothing
Set OutApp = Nothing
Else
MsgBox "The ActiveWorkbook does not have a path, Save the file first."
End If
'Delete the file you have send
Kill TempFilePath & TempFileName & FileExtStr
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End With
End Sub