vendredi 15 novembre 2019

VBA: Set up an if-else condition for Range in the body of mail

I am really new to VBA. I am trying to set up a Makro which does the following thing. When i click it should open an outlook mail, fill the header and body with some information depending on a certain sheet. Parts of that sheet should be also included in the body of the mail. This steps I achieved so far as you can see in the attached code.

Sub Mail_Selection_Range_Outlook_Body()


Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object


Set rng = Union(Range("A5:O8"), Range("A27:O29")).SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If rng Is Nothing Then
    MsgBox "The selection is not a range or the sheet is protected" & _
           vbNewLine & "please correct and try again.", vbOKOnly
    Exit Sub
End If

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


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

On Error Resume Next
With OutMail

    .Subject = Sheets("XXX").Range("XXX").Value & " " & Sheets("XXX").Range("XXX").Value & " " & Sheets("XXX").Range("XXX").Value
    .HTMLBody = "blablabla" & RangetoHTML(rng)
    .Display
End With
On Error GoTo 0

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

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

and

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

My problem is that for the this part

Set rng = Union(Range("XXX"), Range("XXX")).SpecialCells(xlCellTypeVisible)
On Error GoTo 0

I want to include an if else relationship in order to only put the filled cells in the html body. (the filled cells are grouped in 2 parts) For this i already made a function in the excel sheet which shows the amount of the filled cells as a simple number. For example it imagined it somehow like this:

Set rng = ThisWorkbook.Sheets("Angeotsblatt")
With ws
If Range("Z6").Value = "1" Then
    Set rng = .Range("A5:O8").SpecialCells(xlCellTypeVisible)
    Elseif Range("Z6").Value = "2" Then
    Set rng = .Range("A5:O9").SpecialCells(xlCellTypeVisible)
    End If
    End With

But for both ranges that should be included in the html body. I already tried to achieve this by named ranges. Could someone help me with this?

Aucun commentaire:

Enregistrer un commentaire