簡體   English   中英

1004 - 對象工作表的方法范圍失敗

[英]1004 - Method range of object worksheet failed

我為 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 解析為范圍並啟動並運行代碼,但不知何故這些事情並不順利。

下面的行

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不會為空,並且您不會收到由於空白輸入而導致的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM