Conditional formatting in content control and finding Picture Content control in a .docx RRS feed

  • Question

  • Hello,

    I am almost near to complete my word document automation, but still facing 2 problems and I hope you can help me out of this:

    1. Conditional Formatting: I wish to highlight text inside a content control according to their results which is either PASSED or FAILED (please see attachement).

                                object path = aActualResult;                
                                cControl.Range.InsertParagraphBefore();     //cOnctrol if Word.ContentControl type. Here I insert a new paragraph in content control
                                object start = cControl.Range.Start;        //Now I know where it starts
                               Word.Range rb = SummaryDocument.Range(ref start, ref start); //Now I get the para range
                               /** Here is do highlighting stuff **/
                                if (aStatus == "PASSED")
                                    rb.HighlightColorIndex = Word.WdColorIndex.wdBrightGreen;
                                    rb.HighlightColorIndex = Word.WdColorIndex.wdRed;
                                /*****Here I add hyperlink *********/
                                object oTextToDisplay = System.IO.Path.GetFileName(aActualResult);
                                rb.Hyperlinks.Add(rb, ref path, ref MISSING, ref MISSING, ref oTextToDisplay, ref MISSING);

    Unfortunately, the above code does not work. Although I have highlighted the other content controls where the text is replaced instead of appended and it works without any problem and is simple.

    2. Another problem is adding images in a picture content control box. Here is the piece of code I am using

    cControl.Range.InlineShapes.AddPicture(aImagePath, ref MISSING, ref MISSING, ref MISSING);
    //cControl is Word.ContentControl object

    The aove code adds another pic in the control without replacing the older one which I do not want.

    If I iterate through the document to find Microsoft.Office.Tools.Word.PictureContentControl, I does not find the picture content control that I have inserted in template. If I am able to find it then it will be a lot easier because then I can simply replace pic by setting myPicControl.Image

    foreach (Word.ContentControl cControl in SummaryDocument.ContentControls)
      //do something
    //Loop below does not return any of my pic control compnents
    foreach (Microsoft.Office.Tools.Word.PictureContentControl cControl in SummaryDocument.ContentControls)
      //do something

    Thursday, September 5, 2013 4:35 PM


  • Hi Ricky

    Please break this up into two separate questions, as the topics have nothing in common except that they're part of your project... Lumping two very different things together makes it difficult to work on an answer.

    Speaking to the first question, your code is positioning the Range at the start of the range - think of a blinking cursor. So there's nothing in the Range to be highlighted.

     Word.Range rb = SummaryDocument.Range(ref start, ref start); //Now I get the para range
    /** Here is do highlighting stuff **/
    if (aStatus == "PASSED")
    .HighlightColorIndex = Word.WdColorIndex.wdBrightGreen;

    Unfortunately, it's not clear to me what the situation is with the content control (is there text? If yes, how much, what kind, what needs to be highlighted...) at the point where your code snippet starts, so it's difficult for me to make any kind of suggestion to rectify the situation.

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by ricky_CSharp Monday, September 9, 2013 5:41 AM
    Friday, September 6, 2013 2:38 PM