Adding Text and Tables in a single ContentControl?

J

JsJ_Slim

Hi,

First and foremost, I'm actually using C#, however, I'm fine with vba
code/solution. All I need is a nudge in the right direction.

What I currently have is some code that automatically generates a table in a
content control (using the contentControl.Range.ConvertToTable() function).
However, what I want to do now is to add some text *above* the table within
the same content control, eg a title.

I've tried various methods that I can think of, and none of them worked:

* before calling Range.ConvertToTable, add the text at the front of the
string, followed by a new line. (Result: Becomes the first row of the table)

* after calling Range.ConvertToTable, attempted to add text in front the
Range.Text. (Result: Exception - The range cannot be deleted).

By the way, I'm not looking for ways to add text into the table as a title.
I can already do that by merging the cells in the first row, setting it as a
header row, and aligning everything to the center, but that's not what I
want.

Please help!

Thanks!
 
D

Doug Robbins - Word MVP

Your first method was nearly there, but you need to create a new range
object that does not include the added text and convert that new range to a
table.

Dim myrange As Range
With ActiveDocument.ContentControls(1)
.Range.InsertBefore "Table 1" & vbCr
Set myrange = .Range
myrange.start = .Range.Paragraphs(1).Range.End
myrange.ConvertToTable
.Range.Paragraphs(1).Alignment = wdAlignParagraphCenter
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

JsJ_Slim

Hi Doug,

thanks for the help. I too thought it had something to do with InsertBefore.
Will try it out.

Thanks a lot!

Josh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top