dhysong

I am using VSTO SE, and I'm adding multiple content controls to a page on a ribbon button click event. When I add just one contentcontrol, I can begin typing in the control, hit enter and it will go to the next line. However, when I add 2 controls, both controls will not allow me to go to the next line by hitting the enter key. Here's my code:

Code Snippet

object missing = Type.Missing;
object moveUnit = WdUnits.wdCharacter;
object moveLength = 1;

object startRange, endRange;

Range range = p_Application.Selection.Range;
range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
startRange = range.Start;

ContentControl cc1 = p_Application.Selection.Range.ContentControls.Add(WdContentControlType.wdContentControlRichText, ref missing);
cc1.Title = "ContentControl1";
p_Application.Selection.TypeText("ContentControl1");

p_Application.Selection.MoveRight(ref moveUnit, ref moveLength, ref missing);

ContentControl cc2 = p_Application.Selection.Range.ContentControls.Add(WdContentControlType.wdContentControlRichText, ref missing);
cc2.Title = "ContentControl2";
cc2.Range.ParagraphFormat.SpaceBefore = 0;
cc2.Range.ParagraphFormat.SpaceAfter = 0;
cc2.Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle;
cc2.SetPlaceholderText(null, null, "ContentControl2");

Does anyone have an idea why this might be happening

Thanks in advance,

Drew Hysong




Re: Visual Studio Tools for Office ContentControl not allowing multiple lines in word 2007

Ji Zhou – MSFT

Hi Drew,

This is a by-design behavior of RichTextContentControl. If we press enter to let cursor go to next line in the RichTextContentControl, the ContentControl will cover the whole document’s width. So when we put two RichTextContentControl in the same line, Word will restrict us to enter a new line of each control.

The solution is that we should type a new paragraph before we insert the second ContentControl. Codes like:

Code Snippet

object missing = Type.Missing;

object moveUnit = WdUnits.wdCharacter;

object moveLength = 1;

object startRange, endRange;

Range range = p_Application.Selection.Range;

range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;

startRange = range.Start;

ContentControl cc1 = p_Application.Selection.Range.ContentControls.Add(WdContentControlType.wdContentControlRichText, ref missing);

cc1.Title = "ContentControl1";

p_Application.Selection.TypeText("ContentControl1");

p_Application.Selection.MoveRight(ref moveUnit, ref moveLength, ref missing);

p_Application.Selection.TypeParagraph(); //This Line

ContentControl cc2 = p_Application.Selection.Range.ContentControls.Add(WdContentControlType.wdContentControlRichText, ref missing);

cc2.Title = "ContentControl2";

cc2.Range.ParagraphFormat.SpaceBefore = 0;

cc2.Range.ParagraphFormat.SpaceAfter = 0;

cc2.Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle;

cc2.SetPlaceholderText(null, null, "ContentControl2");

Thanks

Ji






Re: Visual Studio Tools for Office ContentControl not allowing multiple lines in word 2007

dhysong

First, thanks for your reply. However, that places the content controls on separate lines, which I don't want. Is there any workaround

Thanks,

Drew






Re: Visual Studio Tools for Office ContentControl not allowing multiple lines in word 2007

Ji Zhou – MSFT

Hi Drew,

Unfortunately, as far as I know, this is the designed layout behavior of ContentControl. So if you use the ContentControl, your objective cannot be done. But what about using two inline text boxes instead

Thanks

Ji






Re: Visual Studio Tools for Office ContentControl not allowing multiple lines in word 2007

dhysong

Thanks Ji for all the help. I found a remedy for my situation by placing the contentcontrols inside a table. This allowed the enter key to go to a new line.

Thanks again,

Drew