【问题标题】:How do i fix: Run time error '404' Object required我该如何解决:运行时错误“404”需要对象
【发布时间】:2021-12-01 10:55:53
【问题描述】:

点击表单按钮会生成10个数字的随机数组,并通过调用模块'SortMas'对其进行排序

   Private Sub CommandButton1_Click()
    Dim i As Integer, b(10) As Single
    Dim CurRange As Range
    ActiveDocument.Paragraphs.Add
ActiveDocument.Content.InsertAfter Text:="Random array:"
ActiveDocument.Paragraphs.Add
    For i = 1 To 10
        b(i) = Int(10 * Rnd + 10)
        ActiveDocument.Content.InsertAfter Text:=Str(b(i)) + " "
    Next i
    
ActiveDocument.Paragraphs.Add

 Text:="Sorted array: "
Call SortMas(b(), 10, 2)
ActiveDocument.Paragraphs.Add
For i = 1 To 10
ActiveDocument.Content.InsertAfter Text:=Str(b(i)) + " "
Next i
End Sub

第 4 行错误: ActiveDocument.Paragraphs.Add

错误信息在第四行代码:

【问题讨论】:

  • 在顶部添加显式选项以查看未声明的变量。我认为您需要参考 Application.ActiveDocument
  • 您会遇到特定的特定错误,因为在 Excel 项目中未定义 ActiveDocument,因为未在模块顶部指定 Option Explicit,因此允许代码运行后期绑定隐式Variant/Empty 变量。使用Option Explicit,编译器会抱怨ActiveDocument 是一个未声明的变量。任何针对 Variant/Empty 的后期绑定成员调用都会抛出错误 424“需要对象”,因为成员调用仅对对象引用有效。
  • 底线,总是在所有模块中指定Option ExplicitRubberduck 会对此发出警告。

标签: vba ms-word


【解决方案1】:

代码在 MS 字中为我工作。 你是用excel还是word? 在 Excel 中它会抛出错误 424,因为段落不是 excel 的一部分,但它在 Word 中效果很好。打开Word---> Atl + F11 ---> 插入一个模块---> 输入正在运行的代码

  Private Sub CommandButton1_Click()
    Dim i As Integer, b(10) As Single
    Dim CurRange As Range
    
'For i2 = 1 To 100
    ActiveDocument.Paragraphs.Add
ActiveDocument.Content.InsertAfter Text:="Random array:"

ActiveDocument.Paragraphs.Add
    For i = 1 To 10
        b(i) = Int(10 * Rnd + 10)
        ActiveDocument.Content.InsertAfter Text:=Str(b(i)) + " "
    Next i
    
ActiveDocument.Paragraphs.Add

 ActiveDocument.Content.InsertAfter Text:="Sorted array: "
'Call SortMas(b(), 10, 2)
ActiveDocument.Paragraphs.Add
For i = 1 To 10
ActiveDocument.Content.InsertAfter Text:=Str(b(i)) + " "
Next i

'Next
End Sub

【讨论】:

  • 你是对的,我是在 microsoft excel 中完成的。谢谢!
猜你喜欢
  • 2021-11-03
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多