none
How to embed an image in a Word document? Excel?

    Question

  • What's the best procedure for embedding an image from an App in a document? The requirement is to flat-out embed the image to allow long-term archiving of documents, and offline distribution on CDs, etc. to non-Internet-connected computers.

    In Word, I'm guessing that this might involve inserting some OOML-typed data, but OOML is not available in Excel.

    Thanks!

    Tuesday, October 02, 2012 12:57 AM

Answers

  • Dear mi/kms; you are right, the way to go is OOXML in Word. you can also use HTML, but as you mentioned in disconnected scenarios this will not fly. (for everyone else i am talking about the setSelectedDataAsync method using different coercionTypes)

    in Excel there is no way to insert an image other than drag and drop from the task pane into the Excel surface, unfortunately.

    hope this helps and thanks for asking...

    Success!!!

    -Juan.


    Thursday, October 04, 2012 6:54 PM

All replies

  • Hi mileskilometers,

    Thanks for posting in the MSDN Forum.

    I will involve some experts into your issue to see whether they can help you. There might be some time delay, appreciate for you patience.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 03, 2012 6:55 AM
  • Dear mi/kms; you are right, the way to go is OOXML in Word. you can also use HTML, but as you mentioned in disconnected scenarios this will not fly. (for everyone else i am talking about the setSelectedDataAsync method using different coercionTypes)

    in Excel there is no way to insert an image other than drag and drop from the task pane into the Excel surface, unfortunately.

    hope this helps and thanks for asking...

    Success!!!

    -Juan.


    Thursday, October 04, 2012 6:54 PM
  • Thanks, Juan.

    So, you're saying that in Excel, dragging & dropping from the task pane is exactly the same as if the user dragged & dropped an image from an independent instance of MSIE? This wold mean we get d&d from the task pane for free...

    Friday, October 05, 2012 11:41 PM
  • Affirmative. You can try the Wikipedia App in Excel, drag and drop an image from the Taskpane to Excel and it will work (btw this is the same case for word)

    thanks!

    Saturday, October 06, 2012 12:04 AM
  • Tried to drag and drop a image from IE to Excel 2013, Excel denies the drop by showing stop icon.
    Saturday, March 02, 2013 3:24 AM
  • FYI, in Word, you can embed the image as binary data inside the OOXML stream, so the image will still be persisted even if you're offline or the file disappears:

    Here's an example of an OOXML part you'd want to insert via setSelectedDataAsync:

    <?xml version="1.0" standalone="yes"?>
    <?mso-application progid="Word.Document"?>
    <pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
      <pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
        <pkg:xmlData>
          <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
            <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
          </Relationships>
        </pkg:xmlData>
      </pkg:part>
      <pkg:part pkg:name="/word/_rels/document.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="256">
        <pkg:xmlData>
          <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
            <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.jpg"/>
          </Relationships>
        </pkg:xmlData>
      </pkg:part>
      <pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
        <pkg:xmlData>
          <w:document mc:Ignorable="w14 w15 wp14" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
            <w:body>
              <w:p w:rsidR="00000000" w:rsidRDefault="00C22E3B" w:rsidP="00C22E3B">
                <w:r>
                  <w:rPr>
                    <w:noProof/>
                  </w:rPr>
                  <w:drawing>
                    <wp:inline distT="0" distB="0" distL="0" distR="0" wp14:anchorId="741DD13B" wp14:editId="36F0B4C1">
                      <wp:extent cx="676275" cy="507206"/>
                      <wp:effectExtent l="0" t="0" r="0" b="7620"/>
                      <wp:docPr id="5" name="Picture 5"/>
                      <wp:cNvGraphicFramePr>
                        <a:graphicFrameLocks noChangeAspect="1" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>
                      </wp:cNvGraphicFramePr>
                      <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                        <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                          <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                            <pic:nvPicPr>
                              <pic:cNvPr id="1" name="Image.png"/>
                              <pic:cNvPicPr/>
                            </pic:nvPicPr>
                            <pic:blipFill>
                              <a:blip r:embed="rId4">
                                <a:extLst>
                                  <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                                    <a14:useLocalDpi val="0" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"/>
                                  </a:ext>
                                </a:extLst>
                              </a:blip>
                              <a:stretch>
                                <a:fillRect/>
                              </a:stretch>
                            </pic:blipFill>
                            <pic:spPr>
                              <a:xfrm>
                                <a:off x="0" y="0"/>
                                <a:ext cx="678661" cy="508996"/>
                              </a:xfrm>
                              <a:prstGeom prst="rect">
                                <a:avLst/>
                              </a:prstGeom>
                            </pic:spPr>
                          </pic:pic>
                        </a:graphicData>
                      </a:graphic>
                    </wp:inline>
                  </w:drawing>
                </w:r>
              </w:p>
              <w:sectPr w:rsidR="00000000">
                <w:pgSz w:w="12240" w:h="15840"/>
                <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
                <w:cols w:space="720"/>
              </w:sectPr>
            </w:body>
          </w:document>
        </pkg:xmlData>
      </pkg:part>
      <pkg:part pkg:name="/word/media/image1.jpg" pkg:contentType="image/png" pkg:compression="store">
        <pkg:binaryData>{REPLACE}</pkg:binaryData>
      </pkg:part>  
    </pkg:package>

    If you replace the {REPLACE} portion at the very end of that XML with your base64 representation of an image, the image should insert just fine.  For example, try replacing it with "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==".  Per the Wikipedia article on base-64 encodings (http://en.wikipedia.org/wiki/Data_URI_scheme), this will add a red dot to your document.

    Of course, you can generate the base-64 strings programmatically, and just do a token-replacement to the OOXML part above.  For example, if you're using server-side .NET code, you could do Convert.ToBase64String and pass in an array of bytes.

    Hope this helps,

    - Michael


    Michael Zlatkovsky | Program Manager, Visual Studio Tools for Office & Apps for Office



    Tuesday, March 05, 2013 10:41 PM
  • Note:  For image size, the cx and cy values are in "EMU" units (for example, see http://openxmldeveloper.org/discussions/formats/f/15/t/396.aspx).  There are 9525 EMUs per pixel, so if you have the pixel dimensions of the image, multiply that pixel value by 9525 for the cx and cy values.


    Michael Zlatkovsky | Program Manager, Visual Studio Tools for Office & Apps for Office

    Tuesday, July 09, 2013 6:44 PM