Rows.Add -> Error 424

N

Newbie

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
 
N

Newbie

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
 
J

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
N

Newbie

<<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
 
J

Jay Freedman

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

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
N

Newbie

Jay,
Thanks a lot for these detailed explanations. Everything is getting clear
for me now.
Newbie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top