[VSTO V3 / Office 2007] Data Binding an Image Content Control

Ok, I'm going to be crazy :)

According to the suggestion provided by Cindy, I'm trying to bind an Image Content Control to a XmlPart

But here is what I have:

Data bindings cannot be created for rich text content controls.

So, what can I do now Is it possible to bind an Image Content Control

If you need to see my code, feel free to ask me :)

Thanks




Answer this question

[VSTO V3 / Office 2007] Data Binding an Image Content Control

  • nad22

    Hello Cindy,

    Here is the code I'm using:

    Word.ContentControlListEntries CommerciauxDropDownList = (Word.ContentControlListEntries)ContentControl.DropdownListEntries;

    foreach (Word.ContentControlListEntry entry in CommerciauxDropDownList)

    {

    if (entry.Text == ContentControl.Range.Text)

    {

    String SelectedId = entry.Value;

    ImageUrl = GetImageUrlForUserId(SelectedId);

    }

    }

    Image image = null;

    HttpWebRequest httpRequest = WebRequest.Create(ImageUrl) as HttpWebRequest;

    httpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;

    HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse;

    using (Stream stream = httpResponse.GetResponseStream())

    {

    image = new Bitmap(stream);

    }

    string ImageText = System.Convert.ToBase64String(ConvertImageToByteArray(image));

    XmlDocument doc = new XmlDocument();

    XmlNode declarationNode = doc.CreateNode(XmlNodeType.XmlDeclaration, String.Empty, String.Empty);

    doc.AppendChild(declarationNode);

    XmlElement imageElement = doc.CreateElement("Image");

    XmlText imageElementValue = doc.CreateTextNode(ImageText);

    doc.AppendChild(imageElement);

    doc.LastChild.AppendChild(imageElementValue);

    object ImageControl = 4;

    Word.ContentControl ImageCommercialControl = this.ContentControls.get_Item(ref ImageControl);

    Office.CustomXMLPart customXmlPart = this.CustomXMLParts.Add(doc.InnerXml, System.Type.Missing);

    try

    {

    ImageCommercialControl.XMLMapping.SetMapping("/Image", String.Empty, customXmlPart);

    }

    catch (Exception)

    { }

    ImageText contains my image converted into base 64.

    All is working fine except that I get an exception (the one I talked you before) on the last line (in the try block)

    Any ideas

    Thanks !



  • Ron L

    A rich text box control is not the same as an image control :-)

    I tested the binding yesterday, so I know it's "allowed" by the Word object model. I inserted an image (or picture, whatever it's labelled in the UI) control. I then did an XMLMapping (as described in the article I pointed you at in a reply, yesterday) to a CustomPart I appended to the document.

    When I loaded a picture into the control in the UI, the base-64 binary code appeared in the CustomPart. No problems.

    What I didn't test was putting a different picture into the CustomPart, primarily because my skills don't extend (yet) to that kind of thing. I come from the Word OM side :-)



  • vgrigor

    Well I know that it's allowed but in my case, it doesn't work...

    Tomorrow, I will show you my code: maybe I've done an error in it..

    Thanks Cindy !



  • Thomas Li

    Another way to bind to a picture content control is to store the image to a temp file and then use addPicture, as in the following snippet of code.

    System.Drawing.Image image = /*Get the image */;
    private global::System.Object missing = global::System.Type.Missing;
    Word.ContentControl pictureContentControl = /* Get the picture content control */;

    string tempImageFileName = Path.GetTempFileName();
    image.Save(tempImageFileName);
    Word.InlineShapes shapes = pictureContentControl.GetRange().InlineShapes;
    while (shapes.Count > 0)
    {
    shapes[1].Delete();
    }
    pictureContentControl.GetRange().InlineShapes.AddPicture(tempImageFileName, ref missing, ref missing, ref missing);
    File.Delete(tempImageFileName);


  • [VSTO V3 / Office 2007] Data Binding an Image Content Control