Solved

Insert Row in Table and Enter text using the range object

Posted on 2013-01-11
4
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 18

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Suggested Courses

732 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