Run-time error 424; Object Required in VBA Word 2010

I created a form in word that used VBA and it has been working for a while.  Now for some reason an error appears when the form opens and makes it impossible to use/edit.  I receive the error popup "Run-time error '424' Object Required".  Below is the code being used, can anyone help me figure out what the error is referring to?  Thank you

Private Sub ComboBox1_DropButtonClick()
ComboBox1.List = Array(" ", "SELECT HERE", "New Hire/Account", "Temp. Employee/Account", "Intern", "Role Change", "Department Move", "Leave of Absence Return")
End Sub
Sub AutoOpen()
Dim myArray() As Variant
Dim sourcedoc As Document
Dim i As Integer
Dim j As Integer
Dim myitem As Range
Dim m As Long
Dim n As Long
  Application.ScreenUpdating = False
  Set sourcedoc = Documents.Open(FileName:="\\data\Public\Roles.docx", Visible:=False)
  i = sourcedoc.Tables(1).Rows.Count - 1
  j = sourcedoc.Tables(1).Columns.Count
  ComboBox2.ColumnCount = j
  'Hide columns 2 and 3
  ComboBox2.ColumnWidths = "75;0;0"
  ReDim myArray(i - 1, j - 1)
  For n = 0 To j - 1
    For m = 0 To i - 1
      Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
      myitem.End = myitem.End - 1
      myArray(m, n) = myitem.Text
    Next m
  Next n
  'Load data into ListBox1
  ComboBox2.List = myArray
  sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
lbl_Exit:
  Exit Sub
End Sub
Private Sub ComboBox2_Change()
 Dim myArray As Variant
   'Use Split function to create an array of data
   myArray = Split(ComboBox2.List(ComboBox2.ListIndex, 1), VBA.Chr(13))
   'Populate listbox2
   ComboBox3.List = myArray
 End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
    Case 46, 48 To 57
    Case Else
        KeyAscii = 0
        MsgBox "Only numbers allowed"
End Select
End Sub

Answer
Answer

That does slightly clarify what should be happening in the code.

The cascading of values from ComboBox2 to ComboBox3 happens in this part of the code:

Private Sub ComboBox2_Change()
 Dim myArray As Variant
   'Use Split function to create an array of data
   myArray = Split(ComboBox2.List(ComboBox2.ListIndex, 1), VBA.Chr(13))
   'Populate listbox2
   ComboBox3.List = myArray
 End Sub

What that does is take the first column of the selected row in ComboBox2, split it into multiple pieces (which are separated by carriage return characters, Chr(13)), and load the pieces into ComboBox3. All of that should work, assuming that the values in ComboBox2 are in the expected form.

The AutoOpen macro should load ComboBox2 with the text found in the table in Roles.docx. In that code, the variable j is set to the number of columns in the table. I still don't see anything wrong with the line of code that you said was highlighted when the error occurred, but it could be related to the contents of the table.

I've also seen situations in which "impossible" errors -- ones that appeared in perfectly good code -- occurred because of corruption of the Windows user profile on the PC. You could try setting up the same document and code on another computer to see whether it still shows the error.

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 6,812 Applies to: