Insert Rows (using Range object)

Z

zSplash

So, here's my latest problem in my quest for Range.objects ("Give me range,
lots of range...")

What code works for inserting rows in a range object, if the following code
works with a Selection object,
Selection.InsertRowsBelow (8)

TIA

(Please forgive my singing -- it's Friday.)
 
D

Dave Lett

Hi,

I don't think you can use .InsertRowsBelow with the range object. The help
topic for this method reads:

An expression that returns a Selection object.

For other insert methods, like .InsertAfter, the help topic for the method
reads:

An expression that returns a Selection or Range object.

HTH,
Dave
 
G

Greg Maxey

Dave,

I reached the same conclusion, but being a novice I elected not to
answer as I there is a lot that I don't know ;-)

I have a quesiton. "an expression that returns a selection object" ...
what else would work besides

Selection.InsertRowsBelow

???
 
Z

zSplash

Thanks, Dave.

So, can you suggest a good work-around, if I don't want to use Selection
objects?
(or, are you already suggesting I use .InsertAfter, and then insert some
new-defined range of one row? Hmm...)

st.
 
D

Dave Lett

Greg,

Being a novice, too, I don't mind so much when I don't get it quite right.
However, I'm relying on the documentation here, so I think I'm relatively
safe.

As for "what else would work besides". I'll venture that the answer is
nothing. I plodded through some of the potential objects but was never able
to drill down to .InsertRowsBelow.

So, I looked up the Selection Object topic in the help file. Here's the
relevant portion, I think:
"Because Range objects share many of the same methods and properties as
Selection objects, using Range objects is preferable for manipulating a
document when there isn't a reason to physically change the current
selection."

When you execute
Selection.InsertRowsBelow (2)
the selection in the interface changes.
Even when you execute inserting a row below from the interface, the physical
selection of the interface changes. Based on this experiential evidence (and
the help files), I'm going to hazard that you MUST use the selection object
to get to InsertRowsBelow.

Dave
 
D

Dave Lett

Hi,

Yes, you _could_ have a workaround, but I'm not a fan of either option.

IF and ONLY IF, the selection is NOT in the last row, then you could use
something like the following:

Dim iCount As Integer
Dim oRng As Range
Set oRng = Selection.Rows(1).Range
oRng.Collapse Direction:=wdCollapseEnd
For iCount = 1 To 8
oRng.Tables(1).Rows.Add BeforeRow:=oRng.Rows(1)
Next iCount

Not too bad, really.
IF your selection is in the last row, then things get more complicated.
Dim iCount As Integer
Dim bLastRow As Boolean
Dim oRng As Range
Dim iRow As Integer
Set oRng = Selection.Rows(1).Range

bLastRow = False
iRow = Selection.Information(wdEndOfRangeRowNumber)
If iRow <> Selection.Tables(1).Rows.Count Then
oRng.Collapse Direction:=wdCollapseEnd
Else
Debug.Print "here"
bLastRow = True
End If
For iCount = 1 To 8
oRng.Tables(1).Rows.Add BeforeRow:=oRng.Rows(1)
Next iCount
If bLastRow Then
oRng.Rows(1).Range.FormattedText =
oRng.Tables(1).Rows(oRng.Tables(1).Rows.Count).Range.FormattedText
oRng.Tables(1).Rows(oRng.Tables(1).Rows.Count).Range.Text = ""
End If
End Sub

It works, but it ain't elegant.

HTH,
Dave
 
Z

zSplash

Thanks, Dave.

Seems like it'd be great, except I get an error ("5991: Cannot access
individual rows in this collection because the table has vertically merged
cells.") (18 rows up)

Instead of "....Add BeforeRow:=oRng.Rows(1)", should I try making oRng be a
row, and then copy and paste it? (I'm working on that possiblity,
unsuccessfully.)

Or, could I just go to the last cell of the table, .InsertAfter "vbLF" 8
times, somehow, programmatically?

st.
 
Z

zSplash

This might be bad coding, but it seems to do the job:
Set oRng = ActiveDocument.Tables(11).Range
oRng.Collapse (wdCollapseEnd)
Set oRow = ActiveDocument.Tables(11).Cell(20, 2).Range 'not last row
oRow.MoveStart wdCell, -1 'oRow is a row
oRow.Copy
For X = 1 To 8
oRng.PasteAndFormat (wdTableInsertAsRows)
Next

(Whew.)

st.
 
Z

zSplash

I spoke too soon. :(

Why can't I just paste (as I do manually) after the Table, and it adds the
row to the table? Is that because I'm (unsuspectingly) using a selection
object when I paste directly into the document?

Uff-dah.

st.
 
Z

zSplash

How about this?
Set oRng = ActiveDocument.Tables(11).Range
oRng.Collapse (wdCollapseEnd)
Set oRow = ActiveDocument.Tables(11).Cell(20, 2).Range
oRow.MoveStart wdCell, -1 'oRow is a row
oRow.Copy
For X = 1 To (8 - numCts)
If X <> 1 Then
oRng.MoveStart wdCharacter, 3 'get back to collapse
End If
oRng.PasteAndFormat (wdFormatOriginalFormatting)
Next

st.
 

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