Tuesday, July 31, 2018

excel - Copy data from worksheet to html file to mail

I gather the data from different Excel sheets and paste the table and content in one sheet and then push that to html file to Outlook.



While pasting the data from the sheet to html file, it is calculating the number of columns in which the data is present.



For Example in one sheet I have pasted text which is around 500 characters on the very first row. On the next row I have pasted a 5*10 table. While copying data to html file it is calculating only 10 columns and copying the data which is in yellow in screenshot.



How do I copy all the data from Excel to html file.




If I use Sheet.UsedRange then on the basis of column it is copying data.



enter image description here



Code:




Dim rng As Range
Dim OutApp As Object

Dim OutMail As Object

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Dim htmlContent
Dim RangetoHTML
Dim lastColumn

Dim lastRow
Dim LastCol
Dim TempFile As String

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

For Each ws In ActiveWorkbook.Worksheets
If (ws.Name "Signature" And ws.Name "URL") Then
Set rng = Nothing
Set rng = ws.UsedRange


lastRow = ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row
Set rng = Range(Cells(1, 1), Cells(lastRow, 20))

'Publish the sheet to a htm file
With ActiveWorkbook.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=ws.Name, _
Source:=ws.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=")



htmlContent = htmlContent & RangetoHTML
'You can also use a sheet name
'Set rng = Sheets("YourSheet").UsedRange
End If
Next ws


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = "sagarwal4@dow.com"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = htmlContent

.Send 'or use .Display
End With
On Error GoTo 0

With Application
.EnableEvents = True
.ScreenUpdating = True
End With

Set OutMail = Nothing

Set OutApp = Nothing

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...