Solved

Insert Row in Table and Enter text using the range object

Posted on 2013-01-11
4
435 Views
Last Modified: 2013-01-11
Dear Experts:

below code nsert a new row below the fifth row of the third table in the active Document.

I do not want to use 'Select' and 'Selection' on this code.

I am sure there is a better way (The 'range' way) to achieve this.

But how?

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

Sub AddRowAndEnterText()

Dim oTable As Word.Table
Dim oRow As Row
  
Set oTable = ActiveDocument.Tables(3)
Set oRow = oTable.Rows(5)
oTable.Rows(5).Select
    Selection.InsertRowsBelow
    Selection.TypeText Text:="This"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="and"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="that"
End Sub

Open in new window

0
Comment
Question by:AndreasHermle
  • 2
4 Comments
 
LVL 17

Expert Comment

by:xtermie
ID: 38767095
This function  adds a row to the end of the table
Private Function addRow(ByRef oRow As Word.Row, Optional iNumberColumns As Integer = 3)
    ' add a row to the end, then work on the one before that
    oTable.Rows.Add
    Set oRow = oTable.Rows(oTable.Rows.Count - 1)
    ' if number of columns is 1 merge 2 to 4, if 2 merge 3 to 4
    Select Case iNumberColumns
        Case 1
            oRow.Cells(2).Merge MergeTo:=oRow.Cells(4)
        Case 2
            oRow.Cells(3).Merge MergeTo:=oRow.Cells(4)
    End Select
End Function
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 38767276
Try:

Sub AddRowAndEnterText2()

Dim oTable As Word.Table
Dim oRow As Row
  
Set oTable = ActiveDocument.Tables(3)
Set oRow = oTable.Rows.Add(beforerow:=oTable.Rows(6))

With oRow
    .Cells(1).Range.Text = "fred"
    .Cells(2).Range.Text = "and"
    .Cells(3).Range.Text = "Doris"
End With
End Sub

Open in new window


Note it unfortunately needs to be "BEFORE" row rather than after!

Chris
0
 

Author Comment

by:AndreasHermle
ID: 38767340
Hi xtermie:

thank you very much for your swift and professional help. Your function will come in handy sometime in the future, but as a matter of fact it did not answer my question. Therefore I will award all the points to Chris. I guess, you can understand this.

Nevertheless, thank you very much for your great support.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 38767342
Hi Chris,

that did the trick. Thank you very much for your professional help. I really appreciate it.

Regards, Andreas
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now