Hi all,
I got some code that helps copy an excel table and create an email from it.
My only problem is that I can't seem to find the way or what part of the code to modify in order to keep the columns width and avoid the table going narrow.
Thanks in advance!
Below both codes I use:
I got some code that helps copy an excel table and create an email from it.
My only problem is that I can't seem to find the way or what part of the code to modify in order to keep the columns width and avoid the table going narrow.
Thanks in advance!
Below both codes I use:
VBA Code:
Sub email1()
Dim OutApp As Object
Dim OutMail As Object
Dim count_row As Integer
Dim pop As Range
Dim str1, str2, str3, str4, EmailTo, EmailCC, EmailBCC, EmailSubject As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
count_row = Range("B124").End(xlDown).Row
Set pop = Sheets("MailMacro").Range("B124:O" & count_row)
str1 = Sheets("Email settings").Range("B7")
str2 = Sheets("Email settings").Range("B8")
str3 = Sheets("Email settings").Range("B10")
str4 = Sheets("Email settings").Range("B11")
EmailTo = Sheets("Email settings").Range("B3")
EmailCC = Sheets("Email settings").Range("B4")
EmailBCC = Sheets("Email settings").Range("B5")
EmailSubject = Sheets("Email settings").Range("B6")
On Error Resume Next
With OutMail
.SendUsingAccount = "x@x.com"
.SentOnBehalfOfName = "x@x.com"
.To = EmailTo
.CC = EmailCC
.BCC = EmailBCC
.Subject = EmailSubject
.Display
.HTMLBody = str1 & "<br>" & "<br>" & str2 & "<br>" & RangetoHTML(pop) & "<br>" & str3 & "<br>" & "<br>" & str4 & "<br>" & "<br>" & .HTMLBody
.DeferredDeliveryTime = Format(DateAdd("d", 7, VBA.Now), "dd-mmm-yyyy") & " 08:00:00 AM"
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
VBA Code:
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, , True, False
'.Cells(1).PasteSpecial xlPasteAllUsingSourceTheme, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'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