Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Rows.Add -> Error 424

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Newbie - 29 Oct 2006 14:12 GMT
Hello,
Could you tell me why the Mytable2 Rows.Add   returns a  424 Error : object
required
Thanks for your help

Sub Tab2()
Dim Mytable2 As Object
Set Mytable2 = ActiveDocument.Tables(2)
MsgBox "Nomber of rows in MyTable2 : " & Mytable2.Rows.Count
Mytable2 Rows.Add       'Runtime error 424 : object required
End Sub
Newbie - 29 Oct 2006 14:34 GMT
Sorry! I found my error :
Mytable2.Rows.Add
I need some vacation... ;-(

Nevertheless, before I take a vacation, could you tell me how to tell that I
want 2 or 3 more rows?

Thanks again

> Hello,
> Could you tell me why the Mytable2 Rows.Add   returns a  424 Error : object
[quoted text clipped - 7 lines]
> Mytable2 Rows.Add       'Runtime error 424 : object required
> End Sub
Jay Freedman - 29 Oct 2006 18:27 GMT
Right, you need the dot to connect the object to the method. Besides
that, if you know that Mytable2 will be a table, you should declare it
with Dim Mytable2 As Table, not As Object. That will make all the
methods and properties of a Table object available in the popup when
you type the dot (MS calls this IntelliSense).

The .Rows.Add method only adds one row at a time. To add more rows,
run the method in a For..Next loop.

You can add more than one at a time with the
Selection.InsertRows(numRows) method. But the Selection must be in the
table, and the inserted rows will be above the selected row, so you
can't use that to add rows to the bottom of the table. The .InsertRows
method belongs only to the Selection, not to any Range object.

If the table already contains at least as many rows as you want to
add, you could copy the desired number of rows to the clipboard, then
position a Range or the Selection just below the bottom of the table
and run the .PasteAppendTable method. If the selected rows had any
contents in the cells, that will be copied too, so you would have to
empty the new rows.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Sorry! I found my error :
>Mytable2.Rows.Add
[quoted text clipped - 17 lines]
>> Mytable2 Rows.Add       'Runtime error 424 : object required
>> End Sub
Newbie - 29 Oct 2006 19:21 GMT
Jay,
Thanks a lot for these detailed explanations.
Newbie

> Right, you need the dot to connect the object to the method. Besides
> that, if you know that Mytable2 will be a table, you should declare it
[quoted text clipped - 46 lines]
> >> Mytable2 Rows.Add       'Runtime error 424 : object required
> >> End Sub
Newbie - 31 Oct 2006 09:57 GMT
<<if you know that Mytable2 will be a table, you should declare it with Dim
Mytable2 As Table, not As Object>>

Jay,
Is it possible to declare such a dim as a Table when the VBA macro is in
Excel and is calling the Word application? (To fill in a Table in Word)
Thanks again,

Newbie

> Right, you need the dot to connect the object to the method. Besides
> that, if you know that Mytable2 will be a table, you should declare it
[quoted text clipped - 46 lines]
> >> Mytable2 Rows.Add       'Runtime error 424 : object required
> >> End Sub
Jay Freedman - 31 Oct 2006 18:48 GMT
Yes, it is.

In the VBA editor in Excel, click Tools > References. Put a check in the box
next to Microsoft Word Object Library. That gives the Excel macro access to
all the objects, methods, and properties of things in Word.

When you're doing cross-application macros, get in the habit of using the
proper qualifiers for all declarations. For this particular example, you
could declare

  Dim myTable As Table

without getting into any trouble. But there are some objects that have the
same name in Excel and in Word (for example, the Style object). To
distinguish between them, you have to write the declaration as

 Dim myStyle As Word.Style

It wouldn't hurt to write

  Dim myTable As Word.Table

just to make clear what it is.

To see what kind of trouble an unqualified declaration can cause, try
running the following example in Excel after setting the reference to Word,
and read the comments:

Sub Demo()
   Dim wApp As Word.Application
   Dim wDoc As Word.Document
   Dim oStyle As Style  ' implicitly Excel.Style
   Dim owStyle As Word.Style
   Dim sName As String

   ' display the Excel styles -- this works
   For Each oStyle In ActiveWorkbook.Styles
       MsgBox oStyle.NameLocal
   Next

   ' start a Word session with a blank doc
   Set wApp = New Word.Application
   wApp.Visible = True
   Set wDoc = wApp.Documents.Add

   On Error Resume Next
   ' this will get a type mismatch error
   ' because oStyle is an Excel style,
   ' not a Word style:
   Set oStyle = wDoc.Styles("Normal")
   If Err.Number > 0 Then
       MsgBox Err.Description
   End If
   On Error GoTo 0  ' clear the error

   ' the following will work because
   ' owStyle is qualified as a Word style
   For Each owStyle In wDoc.Styles
       sName = owStyle.NameLocal
       If LCase(Left(sName, 1)) = "n" Then
           MsgBox sName
       End If
   Next

   wDoc.Close savechanges:=wdDoNotSaveChanges
   wApp.Quit
End Sub

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> <<if you know that Mytable2 will be a table, you should declare it
> with Dim Mytable2 As Table, not As Object>>
[quoted text clipped - 56 lines]
>>>> Mytable2 Rows.Add       'Runtime error 424 : object required
>>>> End Sub
Newbie - 31 Oct 2006 19:40 GMT
Jay,
Thanks a lot for these detailed explanations. Everything is getting clear
for me now.
Newbie

> Yes, it is.
>
[quoted text clipped - 124 lines]
> >>>> Mytable2 Rows.Add       'Runtime error 424 : object required
> >>>> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.