1

Consider this situation: there is a sentence of words above a nested table to be replaced with new text.

I have tried the following :

ActiveDocument.Tables(1).Cell(1, 1).Range.Paragraphs(1).Range.Text = "New Text"

However, the end result is shown in (2)

I want to obtain result (3) ideally without using selection object (including selection.find)

If selection object is used, I have already figured a solution as follows:

ActiveDocument.Tables(1).Cell(1, 1).Range.Paragraphs(1).range.select
selection.typetext "New Text"

Would appreciate your help :)

braX
  • 11,506
  • 5
  • 20
  • 33
  • Have you seen [this question](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba)? It's not specifically about MS Word, but the same principal applies – Nacorid Jan 31 '20 at 13:16
  • I have just seen the link. I am actually well aware of the general benefits of avoiding using selection object before this article. I am more concerned in the specifics of involving replacing text above nested tables (as described). – punggolbaobao Jan 31 '20 at 13:22
  • Did the suggestion in the Answer help? If not, please reply in a comment to that answer describing how it doesn't solve the problem... – Cindy Meister Feb 04 '20 at 14:34

2 Answers2

1

You have to insert a break and then change your text. It's a huge pain, I know, but it's the way that cells and tables as ranges work. Also, your cell text string will be odd because it has a formatting character in it, so you'll have to use Mid to remove that.

Move text out of nested cell into parent cell

Dim myRange as Range
Dim cellText as String
Set myRange = ActiveDocument.Tables(1).Cell(1, 1).Range
cellText = myRange.Text
cellText = Mid$(myRange.Text, 1, Len(myRange.Text) - 2) 'copy text
myRange.Text = vbNullString 'remove text from cell
myRange.InsertBreak wdColumnBreak 'insert break to allow editing of area before range
myRange.InsertBefore cellText 'places text in the area we created with the wdColumnBreak

Use user-input text as label for nested cell

Dim myRange As Range: Set myRange = ActiveDocument.Tables(1).cell(1, 1).Range
Dim cellText As String

cellText = InputBox("Label Text")
myRange.Move wdParagraph, -1
myRange.Paragraphs(1).Range.Text = vbNullString
myRange.InsertBreak wdColumnBreak
myRange.InsertBefore cellText

The concept is the same, but this allows the user to input the new label string.

jclasley
  • 668
  • 5
  • 14
  • I have tried running your code. It did not lead me to my desired outcome. However, I found your comment insightful about the hidden formatting character. – punggolbaobao Feb 17 '20 at 03:15
  • https://i.stack.imgur.com/NRVMv.png [This link is copied from my question above] The desired outcome should be (3) in the image above – punggolbaobao Feb 18 '20 at 15:14
  • https://imgur.com/fci6r6F However, your code results in the image. The text is moved out of the table and the nested table is removed inadvertently – punggolbaobao Feb 18 '20 at 15:15
  • @punggolbaobao I misunderstood the question, I thought that the desire was to move text from the nested cell into the parent cell to act as a label for the nested cell. I'll edit the answer to show how to accomplish your goal. – jclasley Feb 19 '20 at 16:15
  • @punggolbaobao feel free to mark the solution as "accepted" if it solved your problem. This is to help users in the future see which answers worked for the given question. – jclasley Feb 20 '20 at 16:08
0

This may not be an elegant solution but it works (would appreciate any constructive feedback)

ActiveDocument.Tables(1).Range.InsertBefore "New Text" & vbCr
vstart = ActiveDocument.Tables(1).Range.Paragraphs(1).Range.End
vend = ActiveDocument.Tables(1).Range.Paragraphs(2).Range.End
ActiveDocument.Range(vstart - 1, vend).delete
  • This will put the label above the entire table, instead of just the nested table. If you use `Tables(1).Tables(1)` you can refer to the nested table (first table of the first table) – jclasley Feb 19 '20 at 16:25
  • I have just tried running my code and it works. The label really is placed inside the table. I think you made a careless mistake (no offence). But thanks for feedback :) – punggolbaobao Feb 20 '20 at 16:12