Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA Collapsing text in cell range

Status
Not open for further replies.

BrianWen

Programmer
Jun 8, 2009
102
DK
I learned using collapsing ranges when constructing entire documents on this forum a while back. I love the way it works and especially the speed contra building from selection. However, I'm having a hard time now with a table cell.

Usually I collapse the entire document range, add text or whatever I need to add. Then collapse range and add more.

Now, I need to add some varying text in a single table cell. I have this:

<created the table tbl_prices>
With tbl_prices.Rows(2).Cells(3).Range
.Collapse 0
.Text = "First line of text"
.InsertParagraphAfter
.Collapse 0
.Text = "Second line of text"
End with


When this is executed, only the second survives. Why is that?
The reason I need to break this up, is because I need to make some of it bold and more. To my knowledge that cannot be done, had I typed all the text with a single .Text

On a larger scaler (collapsing the entire document range) it works perfectly, but in that table cells, it's like it ignores the .Collapse

Any help or suggestions are appreciated.
 
Hi Brian,

Perhaps a more fundamental problem with your code is that it doesn't insert the text into the nominated cell, but into the next column, if there is one!

Try something along the lines of:
Code:
With tbl_prices..Cell(2, 3).Range
    .InsertParagraphAfter
    .InsertAfter "First line of text"
    .InsertParagraphAfter
    .InsertAfter "Second line of text"
End with

Cheers
Paul Edstein
[MS MVP - Word]
 
I'm not sure what you mean. The code I wrote is from memory, as I don't have the code where I am right now. Anyway, I can easily get text into the correct cell.

Regarding .InsertAfter I sort of can use that, but there are to be more than plain text, I just learned. I need to put in some bullets to.

So like this:

<bold>Headline or whatever</bold>
More text
*bullet 1
*bullet 2
*bullet 3


I simply (stupid me) cannot figure out how put that into a specific table cell.
 
I'm not sure if it's relevant, but I have noticed that in my version of Word (2000), when you paste a whole paragraph into a non-empty table cell, the existing contents are overwritten even if they weren't highlighted. Less than a paragraph behaves as expected and the difference appears to be that Word carries formatting with a paragraph but not with a fragment. I am only talking about behaviour from keyboard / mouse, so it might not apply in code.
 
1. I am not sure why you do the following as separate instructions.
Code:
With tbl_prices.Rows(2).Cells(3).Range
    .Collapse 0
    .Text = "First line of text"
    .InsertParagraphAfter
    .Collapse 0
    .Text = "Second line of text"
End with

Why not?
Code:
With tbl_prices.Rows(2).Cells(3).Range
    .Collapse 0
    .Text = "First line of text" & vbCrLf _
        "Second line of text"
End with

2. "I simply (stupid me) cannot figure out how put that into a specific table cell."

Basically, exactly as you seem to be doing. You can NOT apply formatting during the range operation though. Ranges do not have formats.

<bold>Headline or whatever</bold>
More text
*bullet 1
*bullet 2
*bullet 3

How exactly are you getting this text?

< 60 working days until retirement
 
OK, let me paste some code, now that I've got it:

On this forum I was taught how to, for instance, set a certain word in a paragraph in bold, like this:

Set r = Activedocument.Range
With r
.Text = "Piece of text"
.Collapse 0
lng_rangestart = r.End
.Text = "Piece of text to be bold"
lng_rangeend = r.End
Set rbold = tilbud.Range(Start:=lng_rangestart, End:=lng_rangeend)
.Collapse 0
.Text = "Another piece of text"
rbold.Font.Bold = wdToggle
End With


This gives me: "Piece of text<b>PIece of text to be bold</b>Another piece of text"

This is what I want to do in a table cell with same concept of taking a range and collapsing it, only to add some more and do it again.

Only it seems like I can't do something like:

Set r = Activedocument.Tables(1).Rows(1).Cell(1).Range
With r
.Text = "Piece of text"
.Collapse 0
lng_rangestart = r.End
.Text = "Piece of text to be bold"
lng_rangeend = r.End
Set rbold = Activedocument.Range(Start:=lng_rangestart, End:=lng_rangeend)
.Collapse 0
.Text = "Another piece of text"
rbold.Font.Bold = wdToggle
End With


I just tried this simplified code in a new document and I now see what you meant before, that it proceeds to next column when collapsing. Only I don't understand why it puts the first text in the first cell, the second text (bold) in the second cell in the row and also the third text in the second cell.

Anyway, all this is why I cannot put all text in the text with one .Text, since text inbetween needs to be bold and bullets needs to go. So the question is. How do I put bold text, more text and bullets inside a table cell?
 
Hi Brian,

If you define some appropriate Styles for the paragraph to be bold and for the paragraph with bullets, you can use one .Text expression to insert all the text, then apply the desired Styles to the relevant paragraphs. For example:
Code:
Sub Demo()
With ActiveDocument.Tables(1).Cell(2, 3).Range
  .InsertAfter vbCr & "First line of text" & vbCr & "Second line of text"
  With .Paragraphs.Last
    .Style = "Bullet"
    .Previous.Style = "Bold"
  End With
End With
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
That seems like something I can use. I will look into that. Thank you!
 
Hi Brian,

There are many ways of skinning the cat, so to speak. That's just one of them!

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top