Hello,
I have an Excel file with 2 worksheets. On each worksheet I have installed a button with macros to send the image of the sheet by email. The problem is that on one sheet it is the image of the other that is sent and yet I took the trouble to modify the code as well as the title for the assignment of the button to the correct sheet.
Here is my code for the 1st sheet
In red this is what I changed on the other code for the other sheet. What do I need to change or add?
Thanks
I have an Excel file with 2 worksheets. On each worksheet I have installed a button with macros to send the image of the sheet by email. The problem is that on one sheet it is the image of the other that is sent and yet I took the trouble to modify the code as well as the title for the assignment of the button to the correct sheet.
Here is my code for the 1st sheet
In red this is what I changed on the other code for the other sheet. What do I need to change or add?
Thanks
Rich (BB code):
Sub Mail_small_Text_And_JPG_Range_Outlook_PDCA ()
'Ron de Bruin, 25-10-2019
'This macro use the function named: CopyRangeToJPG
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim MakeJPG As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject ("Outlook.Application")
Set OutMail = OutApp.CreateItem (0)
strbody = "This is what I'm planning today." & "<br> <br>" & _
"Have a nice day! <br>"
'Create JPG file of the range
'Only enter the Sheet name and the range address
MakeJPG = CopyRangeToJPG ("PDCA", "A1: K23")
If MakeJPG = "" Then
MsgBox "Something go wrong, we can't create the mail"
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Exit Sub
End If
On Error Resume Next
With OutMail
.To = "xxx.xxxx@xxxx.com"
.CC = "xxx.xxxxxx@xxxx.com; xxxx.xxxx@xxxx.com;"
.BCC = ""
.Subject = "PDCA of the day"
.Attachments.Add MakeJPG, 1, 0
'Note: Change the width and height as needed
.HTMLBody = "<html> <p>" & strbody & "</p> <img src =" "cid: NamePicture.jpg" "width = 750 height = 800> </html>"
.Display 'or use .Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function CopyRangeToJPG (NameWorksheet As String, RangeAddress As String) As String
'Ron de Bruin, 25-10-2019
Dim PictureRange As Range
With ActiveWorkbook
On Error Resume Next
.Worksheets (NameWorksheet) .Activate
Set PictureRange = .Worksheets (NameWorksheet) .Range (RangeAddress)
If PictureRange Is Nothing Then
MsgBox "Sorry this is not a correct range"
On Error GoTo 0
Exit Function
End If
PictureRange.CopyPicture
With .Worksheets (NameWorksheet) .ChartObjects.Add (PictureRange.Left, PictureRange.Top, PictureRange.Width, PictureRange.Height)
.Activate
.Chart.Paste
.Chart.Export About $ ("temp") & Application.PathSeparator & "NamePicture.jpg", "JPG"
End With
.Worksheets (NameWorksheet) .ChartObjects (.Worksheets (NameWorksheet) .ChartObjects.Count) .Delete
End With
CopyRangeToJPG = About $ ("temp") & Application.PathSeparator & "NamePicture.jpg"
Set PictureRange = Nothing
End Function
Last edited by a moderator: