简体   繁体   中英

Getting Compile Error Function or Sub Function not Defined Excel VBA

I am trying to automate Outlook by a writing a code in Excel. Excel Data Will be Copied in Outlook Mail Body. But when i am trying to run the code i am getting Compile Error ie Function or SubFunction not Defined. Below is the Code :

Private Sub SendMail_Click()


Dim Outlookbj As Object

Dim OutlookMailItem As Object

Dim OutlookAttachment As Object

Dim CopyExcelData As Range

Dim VarDate As String

Dim VarDate1 As String

VarDate = Date

VarDate1 = Left(VarDate, 2)


Set CopyExcelData = Nothing

Set CopyExcelData = Sheets("Sheet1").Range("A1:L27").SpecialCells(xlCellTypeVisible)

If CopyExcelData Is Nothing Then
MsgBox "The Selection is not a range"

End If

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

Set OutlookObj = CreateObject("Outlook.Application")
Set OutlookMailItem = OutlookObj.CreateItem(0)

With OutlookMailItem
.To = "abc@dc.com"
.Subject = "Satus as on " & VarDate1 & " March "
.HTMLBody = RangetoHTML(CopyExcelData)
.Display


End With

End Sub

Private Sub SendMail_Click() is being highlighted in yellow. The Problem is with this line of code. I have also tried by Adding Reference to Solver Add-in. But still I am facing the same issue.

thanks in advance

Start by typing Option Explicit ¹ into the declarations area at the top of the module code sheet (see footnote).

First error: Dim Outlookbj As Object should be Dim OutlookObj As Object

Second error: RangetoHTML(... <~~ this is not a native worksheet or VBA function. It is a UDF (probably the one written by Ron de Bruin). You need the code for it in the module code sheet (see below).

Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
    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

¹ Setting Require Variable Declaration within the VBE's Tools ► Options ► Editor property page will put the Option Explicit statement at the top of each newly created code sheet. This will avoid silly coding mistakes like misspellings as well as influencing you to use the correct variable type in the variable declaration. Variables created on-the-fly without declaration are all of the variant/object type. Using Option Explicit is widely considered 'best practice'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM