
Originally Posted by
xlhelp7
Hi Naveed
Thanks for the reply!
when i run you code it says variable not defined
Regards,
Xlhelp7
hi,
try this code
Sub Email_VBA()
Dim Wkb As Workbook: Set Wkb = ActiveWorkbook
Dim ASht As Worksheet: Set ASht = Wkb.Sheets("Screenshot")
Dim Rng As Range, Rw As Long, LC As Long, Rng2 As Range
Dim SigString As String, Signature As String
With ASht
Set Rng = .Range("A1:Z500")
End With
Dim OApp As Object, OMail As Object, MyStr As Variant
Set OApp = CreateObject("Outlook.Application")
Set OMail = OApp.createitem(0)
Subjct = Wkb.Sheets("Product Report").Range("I22").Value
'======================================
snm = "NAVEED" 'signature name which u have given in outlook
SigString = Environ("appdata") & _
"\Microsoft\Signatures\" & snm & ".htm"
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
'===================================
With OMail
.Display
'.SentOnBehalfOfName = ""
.To = Wkb.Sheets("Product Report").Range("I20").Value
.CC = Wkb.Sheets("Product Report").Range("I21").Value
.Subject = Subjct
'"<P Style='Font-Family:Calibri;Font-Size:11'>"
'.HTMLBody = strbody
.HTMLBody = RangetoHTML(Rng) & "<BR>" & Signature
'.send
End With
Set OMail = Nothing
Set OApp = Nothing
End Sub
Function RangetoHTML(Rng As Range)
Dim fso As Object, ts As Object
Dim TempFile As String, TempWB As Workbook
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
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
'.Columns.AutoFit
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
Function GetBoiler(ByVal sFile As String) As String
'**** Kusleika
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function
Bookmarks