Convert ContentControl to PictureContentControl, possible? RRS feed

  • Question

  • Good morning

    I need to convert a ContentControl into a PictureContentControl in the ContentControl's AfterAdd event, here is what I've tried: 

            void CurrentDocument_ContentControlAfterAdd(Microsoft.Office.Interop.Word.ContentControl cc, bool InUndoRedo)
                if (!String.IsNullOrEmpty(contentControlTitle))
                    Microsoft.Office.Interop.Word.Range lastCaptureRange = windowSelectionRange.FirstOrDefault().Value;
                    Microsoft.Office.Interop.Word.Range range = CurrentDocument.Range();
                    Microsoft.Office.Interop.Word.Font font = range.Font;
                    var name = font.Name;
                    var size = font.Size;
                    Microsoft.Office.Tools.Word.PictureContentControl pictureControl = cc as 
                    pictureControl.Image = CreateImage(contentControlTitle, name, size);
                    pictureControl.LockContents = true;
                    pictureControl.Title = contentControlTitle;
                    pictureControl.Tag = contentControlTitle;
                    range.Start = pictureControl.Range.End + 1;

    This line of code throws a NullReferenceException stating that the PictureContentControl is null,
    in the line just above it however  I set it and cast it to the ContentControl object:

    pictureControl.Image = CreateImage(contentControlTitle, name, size);

    What am i doing wrong?

    • Edited by Denys W Tuesday, January 10, 2012 8:41 AM
    Tuesday, January 10, 2012 6:55 AM

All replies

  • Hi Denys

    See if the information in the section
    To add a content control that is based on a native content control

    on this page

    helps you get further.

    Cindy Meister, VSTO/Word MVP
    Tuesday, January 10, 2012 4:59 PM
  • Hello Cindy

    That doesn't help, everytime the following line of code will be executed it will just re-call the CurrentDocument_ContentControlAfterAdd event:

          this.Controls.AddRichTextContentControl(nativeControl,"VSTORichTextControl" + count.ToString());

    I need some way to cast the given content control into a picture content control.

    Wednesday, January 11, 2012 6:02 AM
  • Hi Denys

    <<I need some way to cast the given content control into a picture content control.>>

    I'm afraid that's not possible.

    Why not add a VSTO content control to begin with, if that's what you need?

    Cindy Meister, VSTO/Word MVP
    Thursday, January 12, 2012 5:36 PM