Send Smtp Email With Condition RangetoHTML Range

Viewed 87

Sending a mail using smtp in macro as automation in excel, kindly help me with the logic for the requirement. Excel Total range is A2:H151 Note: (A1:H1) is header. If the code found a text is not equal to "Connected" From URL Status or Range (H2:H151) then i need the entire row from A to H in the body of the Smtp mail. if code have not find the the text "Connected" means I no need to send a mail. sending a mail only found the text is not equal to connected in "H"

enter image description here

enter image description here

Sub CDO_Mail_Small_Text()
    Dim iMsg As Object
    Dim iConf As Object
    Dim TempFile As String
    'Dim strbody As String
    Dim Flds As Variant
    

    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

      iConf.Load -1    ' CDO Source Defaults
      Set Flds = iConf.Fields
      With Flds
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.ga.account.com"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
      .Update
    End With
    
    With iMsg
        Set .Configuration = iConf
        .To = "aravindaraj@account.com"
        .CC = ""
        .BCC = ""
        .From = """Aravinda"" abc@account.com"
        .Subject = "Important message"
        .HtmlBody = RangetoHTML(Range)
        .Send
    End With

    Set iMsg = Nothing
    Set iConf = Nothing
    Set Flds = Nothing
End Sub

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

    '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
0 Answers
Related