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 xublishsource=", _
"align=left xublishsource=")
'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
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 xublishsource=", _
"align=left xublishsource=")
'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