none
Error with Content Control RRS feed

  • Question

  • Hi,

    Iam using the following code to create a word document and insert a content control in it. If I set the multiline property true for the content control I get the following error:

    Error HRESULT E_FAIL has been returned from a call to a COM component.

     If I do not set the multiline property everything works fine.

    protected void DisplayWord()

    {
    object oMissing = System.Reflection.Missing.Value;
    Word._Application oWord;
    Word._Document oDoc;
    oWord = new Word.Application();
    oWord.Visible = false;
    oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,ref oMissing, ref oMissing);
    Word.Paragraph oPara1;
    oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
    oPara1.Range.Text = "Heading 1";
    oPara1.Range.Font.Bold = 1;
    oPara1.Format.SpaceAfter = 24;
    oPara1.Range.InsertParagraphAfter();

    Word.ContentControl ctrl = oDoc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, oMissing);
    ctrl.LockContents = false;
    //This line causes error
    ctrl.MultiLine = true;
    ctrl.Tag = "1";

    }

     

    Any help would be greatly appreciated.

    Thanks

    Friday, July 22, 2011 4:55 AM

Answers

  • <<Based on your explaination, its my understanding that I cannot include a Content Control with multiline property set to true in the following scenario:

    Twinkle Twinkle [Content Control of type RichText] Star.

    Am I right?>>

    Äh, not exactly. Mulitline is supported - that is, the content control can break over lines. What is not supported is being able to press ENTER - multi-paragraph. That's an important difference :-)

    Please note that it is possible to press Shift+Enter (new line, as opposed to new paragraph). Many scenarios can also be solved by placing the content control and the text in a multi- (usually two-) column table. That puts the content control in its own paragraph, so Enter can be pressed.

    <<Could you please let me know if there are any other controls that would let me

    1. protect certain sections of the document
    2. Allow users to hit enter and jump to the next line and
    3. Format the text within it.>>

    Nothing like that in Word, no...

    <<Based on this this understanding I tried to include a FormField instead of the Control Control in the above example. The form field allows me to press enter and jump to the next line. However, using form fields does not let me bold, Underline, indent or perform other formatting on the form field text.>>

    ...But if you really need exactly that capability it's possible to create macros (only three lines of code, usually) and assign them to buttons and key combinations that will: unprotect-apply formatting to selection-reprotect. The one caveat is that these won't work if the selection is in a table cell.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Tuesday, August 2, 2011 11:05 AM
    Sunday, July 24, 2011 5:10 PM
    Moderator

All replies

  • I'm guessing this is the "FAQ" situation, where the content control is not standing alone in a paragraph. You have other text in the same paragraph? In that case, the content control will not (cannot) support multi-line.

    It has to do with how <tags> are placed in XML. You can't do this with XML elements:

      <tag1><tag2></tag1><tag3></tag3></tag2>

    You see how tag1 "breaks up" tag2? That's not allowed. The elements MUST be nested.

    Since a paragraph is a tag set: <w:p>other tags here</p> and a content control is also a tagset, you can't start the content control in one paragraph that already has text, have that paragraph end, and the content control continue in another paragraph.

    If the content control stands in its own paragraph and ENTER is pressed, then in the UI you'll see that the paragraph mark is incorporated into the content control instead of being outside it, as it was originally. This means that the content control tagset will surround all the paragraph marks in the mult-line, rather than a single paragraph mark surrounding the content control.


    Cindy Meister, VSTO/Word MVP
    Friday, July 22, 2011 6:51 AM
    Moderator
  • Cindy,

    Based on your explaination, its my understanding that I cannot include a Content Control with multiline property set to true in the following scenario:

    Twinkle Twinkle [Content Control of type RichText] Star.

    Am I right? Based on this this understanding I tried to include a FormField instead of the Control Control in the above example. The form field allows me to press enter and jump to the next line. However, using form fields does not let me bold, Underline, indent or perform other formatting on the form field text.

    Could you please let me know if there are any other controls that would let me

    1. protect certain sections of the document

    2. Allow users to hit enter and jump to the next line and

    3. Format the text within it.

     

    Thanks for all your help.

     

    Saturday, July 23, 2011 8:29 PM
  • <<Based on your explaination, its my understanding that I cannot include a Content Control with multiline property set to true in the following scenario:

    Twinkle Twinkle [Content Control of type RichText] Star.

    Am I right?>>

    Äh, not exactly. Mulitline is supported - that is, the content control can break over lines. What is not supported is being able to press ENTER - multi-paragraph. That's an important difference :-)

    Please note that it is possible to press Shift+Enter (new line, as opposed to new paragraph). Many scenarios can also be solved by placing the content control and the text in a multi- (usually two-) column table. That puts the content control in its own paragraph, so Enter can be pressed.

    <<Could you please let me know if there are any other controls that would let me

    1. protect certain sections of the document
    2. Allow users to hit enter and jump to the next line and
    3. Format the text within it.>>

    Nothing like that in Word, no...

    <<Based on this this understanding I tried to include a FormField instead of the Control Control in the above example. The form field allows me to press enter and jump to the next line. However, using form fields does not let me bold, Underline, indent or perform other formatting on the form field text.>>

    ...But if you really need exactly that capability it's possible to create macros (only three lines of code, usually) and assign them to buttons and key combinations that will: unprotect-apply formatting to selection-reprotect. The one caveat is that these won't work if the selection is in a table cell.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Tuesday, August 2, 2011 11:05 AM
    Sunday, July 24, 2011 5:10 PM
    Moderator
  • Hi VKM91,

    Have you figured out your problem? If you still need any assistant, just feel free to let us know.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 29, 2011 4:13 AM