none
Image file name is changed when set to content control RRS feed

  • Question

  • Hello All,

     

    I use the following code to set an image from file to content control. However, Word changes the file name when stores it to media folder. For some reasons, I need to know the file name in Word or keep its original name when set to content control. Are there any ways to do this?

     

    control.Range.InlineShapes.AddPicture(strFilePath);
    

    I appreciate for your helps.

     

    Regards,

     

    Vu

    Sunday, April 24, 2011 2:34 PM

All replies

  • Hi Vu,

    I believe something like this might work for you:

    With

     

    wApp.Selection.Range.InlineShapes

    .AddPicture(

    "My Path").Title =

    "MyName"

     

    End

    With


    Regards
    Sunday, April 24, 2011 3:03 PM
  • Hi RichMichaels,

    Thanks for your reply. I can't find Title property of InlineShape. I'm using Microsoft.Office.Interop.Word. Do you have any idea about this?

    There is a mistake in my last post. The code to set image to content control is:

    control.Range.InlineShapes.AddPicture(strFilePath, ref missing, ref missing, ref missing);

    Regards,

    Vu

    Sunday, April 24, 2011 3:48 PM
  • it is there (see http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshape.title.aspx for reference). I believe RichMichaels example is in vb.net and not c# if you are confused about the syntax.
    Andreas M.
    Sunday, April 24, 2011 6:29 PM
  • Hi Vu,

    Andreas is correct, I provide a VB example. I can read C# but not write it ... I don't know the syntax.


    Regards
    Sunday, April 24, 2011 10:39 PM
  • Thanks Rich and MaieV.

    It seems that Title property is available on Office 2010. I'm using Office 2007. Is there a same property on Office 2007?

    Monday, April 25, 2011 1:48 AM
  • Hi Vu,

    Unfortunately no. However, there is an AlternativeText property and I'm wondering if that might suit your purpose for identifying the file.


    Regards, Rich
    Monday, April 25, 2011 2:44 AM
  • Hi Rich,

    AlternativeText doesn't work for me.

    I set

    control.Range.InlineShapes.AddPicture(strFilePath, ref missing, ref missing, ref missing).AlternativeText = "IO50.png";

    but the image name in media folder of Word is still image1.png.

    Do you have other ideas for this situation?

    Regards,

    Vu

    Monday, April 25, 2011 3:46 AM
  • Hi Vu

    There isn't going to be any direct way for you to force Word to save the information of your choice within the Open XML file format when you work through the Word UI. Best you can do is store that information elsewhere and then "look it up" when you need it.

    One possibility would be to write the file name to the content control's TAG property.

    Another could be to link the content control to a Custom XML Part stored in the document, then write the file name to an attribute of that part. A variation on the approach would be to include a Custom XML Part, not linked to the content control, and store the content control's ID property along with the file name (and any other information you consider important).

    Further, Word has the concept of "Document Variables". These are storage areas for strings within the Word document structure (not visible to the user). It's a key/value pair kind of thing. You could store the file information in such a Document.Variable object.


    Cindy Meister, VSTO/Word MVP
    Monday, April 25, 2011 5:28 AM
    Moderator
  • Hi Cindy,

    Thanks for your idea about my issue.

    I had a tag in Custom XML stores the image file name already. However, when I set the image from file to content control, the image is stored in Word with a different name with the value in Custom XML. For example, image name value in Custom XML is "IO50.png", but the image in Word is saved as "image1.png". After I create Word file, another module will extract Custom XML and images from Word file to use. In this case, because of the difference between image name value in Custom XML and the image files extracted from Word, there are some incorrect logic occur.

    I think it's impossible to force Word to store the image with my expected name. However, I wonder if are there any ways to retrieve the image name which is drawn in content control? I can use this value to update back to Custom XML. By this way, name value in Custom XML and image in Word are same. For example, same to "image1.png".

    I appreciate your idea.

    Regards,

    Vu

    Monday, April 25, 2011 8:24 AM
  • Hi Vu,

    Now it's becoming clearer what you are trying to do. I'm no expert on accessing the XML files programmatically but I think that's what you're going to need to do.

    In the document.xml file you can see all of the images that are inserted into the document. Their original name is there as well as a system generated name. It looks like this.

    <wp:docPr id="1" name="Picture 0" descr="LGPro72010.png" />

    In the above example "Picture 0" translates to image1.png in the media folder and the descr field holds the file's original name that I inserted in my test document.

    Hopefully this will get you pointed in a direction to find a solution that works for you.

    As I said, I'm not familiar with how you would programatically parse this xml file and extract the information you need but maybe others will offer help.


    Regards, Rich
    Monday, April 25, 2011 12:20 PM
  • I think what Cindy was trying to say is that when you include an image using the Word object model, there is no way to specify the name of the image part in the .docx file. That name, and many other names in the .docx file are determined by Word.

    You can certainly manipulate the XML directly as Rich suggests, but in this case

     a. I think you would have to do that after you had saved and closed the file, using something other than the Word Object model.

     b. what to change would depend on exactly how the image is represented in the XML. Historically, there is more than one. I would guess that in most cases,

         - word/document.xml would contain the ID of a relationship, 

         - word/_rels/document.xml.rels would contain a Relationship element that would define a relationship with that ID, a suitable relationship type, and a target of word/media/image1.jpg

         - in that case, you would need to change the name of the image.jpg part, and change the target of the Relationship element. In some cases, you might have to modify [Content_Types].xml, although I doubt it.

         - the example Rich has posted uses a different way to reference the relationship - without checking, I think it is more likely that the id="1" identifies the relationship, not the "Picture 0"

     c. the problem is that all a user has to do is open that document, modify it in some way, and save it, and Word will re-impose all its standard naing conventions all over again.

     


    Peter Jamieson
    Monday, April 25, 2011 6:03 PM
  • Thanks Rich and Peter for your valuable advices.

    This is really a possible solution. As Peter mentions, I can't do this way when I create the document. I think it is a limitation. Don't we have any properties or methods provided by Word object model to access the relationship?

    However, I have something to try. Thanks again for your helps.

    Tuesday, April 26, 2011 2:06 AM
  • > Don't we have any properties or methods provided by Word object model to access the relationship?
    No - at least if there are, I don't know of them.
    The closest thing I know to altering the XML of a document directly is to use InsertXML to "inject" XML into the document, but even in that case, Word does what it wants with the XML. As an example, you can try the following - even though it looks as if you might be altering the name of the "image1.jpg" part, Word actually immediately re-imposes its standard naming convention.
    Sub TryToModifyImageXML()
    Dim s As String
    Dim t As String
    ' Before running this macro,
    ' 1. create and save a .docx containing
    '    a single image
    ' 2. select the image
    ' 3. step through the macro in debug mode
    ' 5. notice that "myphoto" does not appear in the
    '    extracted XML
    ' 6. if you want, inspect the XML content of the
    '    modified file to be sure!
    ' Get the image XML in Flat OPC format
    s = Selection.Range.WordOpenXML
    ' look for "image1"
    If InStr(s, "image1") > 0 Then
      Debug.Print "Found image1"
      ' modify all references to "image1"
      t = Replace(s, "image1", "myphoto")
      ' delete the existing image
      Selection.Range.Cut
      ' ensure the image has gone
      ActiveDocument.Select
      ' re-extract the XML
      s = Selection.Range.WordOpenXML
      ' look for "image1"
      If InStr(s, "image1") > 0 Then
        Debug.Print "image1 is still there :-("
      Else
        Debug.Print "Image has been deleted :-)"
      
        ' insert the modified XML
        Selection.Range.InsertXML t
        ActiveDocument.Save
        ' ensure the image is selected
        ActiveDocument.Select
        ' re-extract the XML
        s = Selection.Range.WordOpenXML
        ' look for "myphoto"
        If InStr(s, "myphoto") > 0 Then
          Debug.Print "Found myphoto :-)"
        End If
        ' look for "image1"
        If InStr(s, "image1") > 0 Then
          Debug.Print "Found image1 again :-("
        End If
        ActiveDocument.Close
      End If
    Else
      Debug.Print "Could not find the text ""image1"""
    End If
    End Sub

    Peter Jamieson
    Tuesday, April 26, 2011 7:06 AM
  • Thanks Peter.
    Tuesday, April 26, 2011 7:23 AM