1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| Option Explicit
Public Function ReadFile(sFileName) As String
Dim fso As Object, fFile As Object
Dim sTemp As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set fFile = fso.OpenTextFile(sFileName, 1, False)
sTemp = fFile.ReadAll
fFile.Close
Set fFile = Nothing
ReadFile = sTemp
End Function
'---Open Outlook type stuff---'
Sub PrepareOutlookMail(ByVal sFileName As String)
Dim appOutlook As Outlook.Application
Dim Mail As Outlook.MailItem
Set appOutlook = CreateObject("Outlook.Application")
If Not (appOutlook Is Nothing) Then
Set Mail = appOutlook.CreateItem(olMailItem)
Mail.HTMLBody = ReadFile(sFileName)
Mail.Display
'---Recipients---'
Mail.Recipients.Add ("email@email.com")
'---Subject---'
Mail.Subject = "Mise à jour stock consommables"
Mail.Display
Mail.Send
Set Mail = Nothing
Set appOutlook = Nothing
End If
End Sub
'---Body Mail---'
Sub SendRangeByMail()
Dim rngeSend As Range
With Application
On Error Resume Next
Set rngeSend = Range("A1:P45")
If rngeSend Is Nothing Then Exit Sub
On Error GoTo 0
.ActiveWorkbook.PublishObjects.Add(4, "E:\XLRange.htm", rngeSend.Parent.Name, rngeSend.Address, 0, "", "").Publish True
Call PrepareOutlookMail("E:\XLRange.htm")
Kill "E:\XLRange.htm"
End With
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
'---Message box---'
Dim answer As String
answer = MsgBox("Voulez-vous enregistrer le fichier", vbYesNo, "Enregistrement")
If answer = vbNo Then Cancel = True
If answer = vbYes Then
SendRangeByMail
'---Give conformation of sent message---'
MsgBox "Le fichier a bien été enregistré", , "Fichier enregistré"
End If
'---Save the document---'
'---Me.Worksheets.Save---'
End Sub |
Partager