Home » SharepointRSS

Microsoft.Office.Tools.Word.ContentControl objects in a Add-In Project, is that even possible?

Hi everyone , 

By all the information that i could find in msdn, it seems that objects in the package Microsoft.Office.Tools.Word, e.g. the Content Control types, must either be added at design (compile) time within Visual Studio in a Document project type , or added at run time. 

Is there any way that i could simply add the content controls using the "Develop" tab in Office Word , and then find the object as a ContentControl, or even something like a PiectureContentControl?

 

And my final goal is to find the Picture Content Controls in the Word document and update the image of the control. 

 

Thanks a lot!

 

5 Answers Found

 

Answer 1

Hi ramboisyuanbo

Yes, the Word "interop", used in an Add-in, can work with content  controls that already exist in a document. The object  model provides the Document.SelectContentControlsByTitle and Document.SelectContentControlsByTag methods. Both return a collection of content controls  having the same title resp. tag.

Word also assigns each content control  an ID value (read-only). You can pick up a specific content control using that as the Index value.

You can also get a content control from a range if, for example, you've applied a bookmark to it.

 

Answer 2

Hi Cindy, 

Thanks a lot for your answer!

I've also find  that i can "add" an temporary ContentControl "based" on an interop content  control, which seems to work  like an wrapper around the interop content control. Is my understanding right?

 

But still there's an weird issue here. This is my code snippet:

 

  Microsoft.Office.Interop.Word.Document doc = 

                Globals.ThisAddIn.Application.ActiveDocument;

 

            Microsoft.Office.Tools.Word.Document vstoDoc = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);

            int index = 1;

            foreach (Microsoft.Office.Interop.Word.ContentControl nativeControl in doc.ContentControls)

            {

                if (nativeControl.Type ==

                    Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlPicture)

                {

                    Microsoft.Office.Tools.Word.PictureContentControl tempControl =

                        vstoDoc.Controls.AddPictureContentControl(nativeControl,

                        "VSTORichTextControl" + (index++).ToString());                   

 

                    //Image image  = Clipboard.GetImage();

                    Image image = new System.Drawing.Bitmap("d:/a.bmp", true);

                    tempControl.Image = image;

 

                    System.Windows.Forms.MessageBox.Show(tempControl.Image.Size.ToString());

                }

            }

 

This code works well, i can see the picture  content control's image is successfully set. 

However, If instead of using an Image object  loaded from the file, if I get the Image from the ClipBoard, the code still runs ok but i can't see the picture content control's Image is changed. I believe the image i got from the clipboard is definitely valid, since  this line of code always gives proper result:

                    System.Windows.Forms.MessageBox.Show(tempControl.Image.Size.ToString());

 

 

Many thanks Cindy!

 

 

Answer 3

<<I've also find  that i can "add" an temporary ContentControl "based" on an interop content  control, which seems to work  like an wrapper around the interop content control. Is my understanding right?>>

Yes.

<<However, If instead of using an Image object  loaded from the file, if I get the image  from the ClipBoard, the code still runs ok but i can't see the picture  content control's Image is changed. I believe the image i got from the clipboard is definitely valid, since  this line of code always gives proper result:

                    System.Windows.Forms.MessageBox.Show(tempControl.Image.Size.ToString());>>

Two things to test

1. Does it work if you do the paste as an end-user into the control?

2. If the image is pasted into a "plain" part of the document  (not a content control) from your code, is it then visible?

 

Answer 4

Hi ramboisyuanbo,

How is this issue going on in your side? Have you solved the problem?

Best Regards,
Amy Li
 

Answer 5

Dear Amy and Cindy,

 

It works if i paste as an end user. 

After i went through the code step by step, it seems i always get null from Clipboard.GetImage();

My current solution is like this, where contentControl is the picture  control.

contentControl.Range.Select();

contentControl.Range.Paste();

 

Thanks a lot!

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter