Right an E-mail with tablem and Image

fernandotinoco

New Member
Joined
Jul 15, 2014
Messages
4
I have the code bellow to create an e-mail.
My code works fine to edit, select and paste the table into the e-mail.
My problem is to copy images from the htm file to the e-mail.
The htm file has the images exactly as I need.



Function RangetoHTML(rng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook


TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"


'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With

Range("A1").EntireRow.RowHeight = 4.5
Range("A67").EntireRow.RowHeight = 4.5




'Delete excess rows
Range("D66").End(xlUp).Select
linha_ativa = ActiveCell.Row
Range("D61:D" & linha_ativa + 1).EntireRow.Select
Selection.Delete Shift:=xlUp

Call InsertImageCabecalho
Call InsertImageLogo

Range("B13").End(xlDown).Select
linha_ativa = ActiveCell.Row
Range("A1:E" & linha_ativa + 6).Select

'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With

'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.Getfile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")


'Close TempWB
TempWB.Close savechanges:=False


'Delete the htm file we used in this function
Kill TempFile


Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function

______________________________________________________________________________________

Sub InsertImageCabecalho()
InsertCabecalho "Z:\VBA HTML\Cabecalho.png", _
Range("B2:E8")
End Sub
_____________________________________________________________________________________________
Sub InsertCabecalho(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = 585
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
'.Width = w
' .Height = h
End With
Set p = Nothing
End Sub
_______________________________________________________________________________
Sub InsertImageLogo()

Range("D66").End(xlUp).Select
linha_ativa = ActiveCell.Row

InsertLogo "Z:\VBA HTML\BTG Pactual logo.png", _
Range("D" & linha_ativa + 2, "E" & linha_ativa + 5)
End Sub
________________________________________________________________________________
Sub InsertLogo(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = 585
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
' .Width = w
' .Height = h
End With
Set p = Nothing
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Write an E-mail with table and Image

_____________________________
_____________________________

I have the code bellow to create an e-mail.
My code works fine to edit, select and paste the table into the e-mail.
My problem is to copy images from the htm file to the e-mail.
The htm file has the images exactly as I need.



Function RangetoHTML(rng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook


TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"


'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With

Range("A1").EntireRow.RowHeight = 4.5
Range("A67").EntireRow.RowHeight = 4.5




'Delete excess rows
Range("D66").End(xlUp).Select
linha_ativa = ActiveCell.Row
Range("D61:D" & linha_ativa + 1).EntireRow.Select
Selection.Delete Shift:=xlUp

Call InsertImageCabecalho
Call InsertImageLogo

Range("B13").End(xlDown).Select
linha_ativa = ActiveCell.Row
Range("A1:E" & linha_ativa + 6).Select

'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With

'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.Getfile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")


'Close TempWB
TempWB.Close savechanges:=False


'Delete the htm file we used in this function
Kill TempFile


Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function

______________________________________________________________________________________

Sub InsertImageCabecalho()
InsertCabecalho "Z:\VBA HTML\Cabecalho.png", _
Range("B2:E8")
End Sub
_____________________________________________________________________________________________
Sub InsertCabecalho(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = 585
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
'.Width = w
' .Height = h
End With
Set p = Nothing
End Sub
_______________________________________________________________________________
Sub InsertImageLogo()

Range("D66").End(xlUp).Select
linha_ativa = ActiveCell.Row

InsertLogo "Z:\VBA HTML\BTG Pactual logo.png", _
Range("D" & linha_ativa + 2, "E" & linha_ativa + 5)
End Sub
________________________________________________________________________________
Sub InsertLogo(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = 585
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
' .Width = w
' .Height = h
End With
Set p = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top