1004-对象工作表的方法范围失败

时间:2018-12-14 10:19:47

标签: excel vba

我有一个小问题,我为Excel编写了一个宏,但是有一个错误,我不知道解决该错误的解决方案。这是代码:

Dim poleInput As Variant

Public Function HasContent(text_box As Object) As Boolean
    HasContent = (Len(Trim(text_box.Value)) > 0)
End Function

Sub TextBox1_Change()
        poleInput = TextBox1.Text
End Sub

Sub CommandButton1_Click()
    If HasContent(TextBox1) Then
        MsgBox "Po¾e je prázne, pridaj nejake údaje!"
    Else
        'MsgBox (poleInput)
        AddAppointments (poleInput)
        AddAppointmentsAfterThreeMonths (poleInput)
        MsgBox "Pripomienka úspešne poslatá!"
    End If
End Sub

Sub AddAppointments(pole As String)
'Update by Extendoffice 20180608
    Dim I As Long
    Dim xRg As Range
    Dim xOutApp As Object
    Dim xOutItem As Object

    Set xOutApp = CreateObject("Outlook.Application")
    Set xRg = Range(pole)

    For I = 1 To xRg.Rows.Count
        Set xOutItem = xOutApp.createitem(1)
        Debug.Print xRg.Cells(I, 1).Value
        xOutItem.Subject = "Posla mail " & xRg.Cells(I, 2).Value
        xOutItem.Location = "Office"
        xOutItem.Start = xRg.Cells(I, 1).Value & " 11:00"
        xOutItem.End = xRg.Cells(I, 1).Value & " 17:00"
        xOutItem.BusyStatus = 2
        xOutItem.ReminderSet = True
        xOutItem.ReminderMinutesBeforeStart = "15"
        xOutItem.Body = "Posla mail zamestnancovy " & xRg.Cells(I, 2).Value
        xOutItem.Save
        Set xOutItem = Nothing
    Next
    Set xOutApp = Nothing
End Sub

Sub AddAppointmentsAfterThreeMonths(pole As String)
'Update by Extendoffice 20180608
    Dim I As Long
    Dim xRg As Range
    Dim xOutApp As Object
    Dim xOutItem As Object

    Set xOutApp = CreateObject("Outlook.Application")
    Set xRg = Range(pole)

    For I = 1 To xRg.Rows.Count
        Set xOutItem = xOutApp.createitem(1)
        Debug.Print xRg.Cells(I, 1).Value
        xOutItem.Subject = "Posla pripomienku " & xRg.Cells(I, 2).Value
        xOutItem.Location = "Office"
        xOutItem.Start = DateAdd("m", 3, xRg.Cells(I, 1)) & " 11:00"
        xOutItem.End = DateAdd("m", 3, xRg.Cells(I, 1)) & " 17:00"
        xOutItem.BusyStatus = 2
        xOutItem.ReminderSet = True
        xOutItem.ReminderMinutesBeforeStart = "15"
        xOutItem.Body = "Posla pripomienku zamestnancovy " & xRg.Cells(I, 2).Value
        xOutItem.Save
        Set xOutItem = Nothing
    Next
    Set xOutApp = Nothing
End Sub

此行显示错误:

Set xRg = Range(pole)

我不明白为什么会显示问题,这很容易,它只应将String解析为范围并启动并运行代码,但是以某种方式使这些事情进展顺利,因此,如果您中有人知道该问题的解决方案,请向我发布解决方案,< / p>

非常感谢。

1 个答案:

答案 0 :(得分:0)

下面一行

If HasContent(TextBox1) Then

相同
If HasContent(TextBox1) = True Then

这不是您想要的。您要避免一个空的文本框。换行

If HasContent(TextBox1) Then

If HasContent(TextBox1) = False Then

或到

If Not HasContent(TextBox1) Then

示例

If Not HasContent(TextBox1) Then '<~~ Blank
    MsgBox "Blank"
Else '<~~ Not Blank
   MsgBox "Not Blank"
End If

这样poleInput不会为空,也不会收到由于输入空白而导致的错误。