Solved

Insert Row in Table and Enter text using the range object

Posted on 2013-01-11
4
464 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 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

820 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