locked
How to Insert Content Control into Word Document RRS feed

  • Question

  • Hi,

    I am trying to develop an app for word 2013 and want to know following.

    1) How I can insert a content control on click of a button. I am not able to figure out which API to use.

    2) Is it possible to save the entire word doc to a server location from within the task pane app?

    Thanks.

    Saturday, September 22, 2012 12:40 AM

Answers

  • Dear AzureLearner: this is a great question thanks for asking!

    The quick answer for 1) is YES! you can add a content control with a click of a button. there are 2 ways of doing this, depending on your scenario one could work better than the other one.

    Solution 1.A) You can use OOXML(Office Open XML).  apps in word have the capability of inserting and OOXML string into the document. All you need to is execute a Office.context.document.setSelectedDataAsync use the coercionType 'ooxml' and make sure to send a valid OOXML with the content control definition. All objects in word can be represented with OOXML including content controls (this is the same for charts, wordArt, etc.). The easiest way to get the OOXML is to actually perform a getSelectedDataAsync operation on whatever object you want to get the OOXML you need, then just replicate the OOXML with the specific settings you need.

    Here is a sample on how you can do this:

    var tempData;
    function readData() {
        Office.context.document.getSelectedDataAsync(Office.CoercionType.Text, {}, function (result) {
            if (result.status == 'succeeded')
                tempData = result.value;
        });
    }

    function writeData() {
        Office.context.document.setSelectedDataAsync(tempData, { coercionType: 'ooxml' });
    }

    Solution 1.b) You can create a binding, and once created, read and write data to the content control by using the binding.getDataAsync/setDataAsync methods. Just create a binding on the currrent selection and that inserts a content control, the issue here is that you cannot set specific properties of the content control, if you want to have this level of control, you need to go with option a) above.

    for your second question: Its not possible to save the document directly in a server location with the API, but you can have access to the whole document as either text or binary formats and then using JS upload it to where you need it. So your scenario is completely achievable! you can have access to the whole document using the Office JavaScript API. Its important to note though that this is specific feature will change a bit between the office.js we shipped on the preview and the final release to market (RTM) version of Office.js, we improved a lot how this will work in RTM, so this is good news.

    for the preview i recommend you to see this article on got to use the preview API.

    http://msdn.microsoft.com/en-us/library/office/apps/fp179810(v=office.15).aspx

    we will update the help on the new API as soon as the new version of the API is avialable.

    thanks for coding apps !!

    • Proposed as answer by Russell Palmer Friday, September 28, 2012 7:05 PM
    • Marked as answer by Forrest Guo Saturday, September 29, 2012 3:37 AM
    Friday, September 28, 2012 12:36 AM

All replies

  • Hi,

    I'm looking into this query, will update you soon.

    thanks.


    Forrest Guo | MSDN Community Support | Feedback to manager

    Tuesday, September 25, 2012 2:35 AM
  • Thanks. Looking forward to your update.
    Tuesday, September 25, 2012 4:49 AM
  • Dear AzureLearner: this is a great question thanks for asking!

    The quick answer for 1) is YES! you can add a content control with a click of a button. there are 2 ways of doing this, depending on your scenario one could work better than the other one.

    Solution 1.A) You can use OOXML(Office Open XML).  apps in word have the capability of inserting and OOXML string into the document. All you need to is execute a Office.context.document.setSelectedDataAsync use the coercionType 'ooxml' and make sure to send a valid OOXML with the content control definition. All objects in word can be represented with OOXML including content controls (this is the same for charts, wordArt, etc.). The easiest way to get the OOXML is to actually perform a getSelectedDataAsync operation on whatever object you want to get the OOXML you need, then just replicate the OOXML with the specific settings you need.

    Here is a sample on how you can do this:

    var tempData;
    function readData() {
        Office.context.document.getSelectedDataAsync(Office.CoercionType.Text, {}, function (result) {
            if (result.status == 'succeeded')
                tempData = result.value;
        });
    }

    function writeData() {
        Office.context.document.setSelectedDataAsync(tempData, { coercionType: 'ooxml' });
    }

    Solution 1.b) You can create a binding, and once created, read and write data to the content control by using the binding.getDataAsync/setDataAsync methods. Just create a binding on the currrent selection and that inserts a content control, the issue here is that you cannot set specific properties of the content control, if you want to have this level of control, you need to go with option a) above.

    for your second question: Its not possible to save the document directly in a server location with the API, but you can have access to the whole document as either text or binary formats and then using JS upload it to where you need it. So your scenario is completely achievable! you can have access to the whole document using the Office JavaScript API. Its important to note though that this is specific feature will change a bit between the office.js we shipped on the preview and the final release to market (RTM) version of Office.js, we improved a lot how this will work in RTM, so this is good news.

    for the preview i recommend you to see this article on got to use the preview API.

    http://msdn.microsoft.com/en-us/library/office/apps/fp179810(v=office.15).aspx

    we will update the help on the new API as soon as the new version of the API is avialable.

    thanks for coding apps !!

    • Proposed as answer by Russell Palmer Friday, September 28, 2012 7:05 PM
    • Marked as answer by Forrest Guo Saturday, September 29, 2012 3:37 AM
    Friday, September 28, 2012 12:36 AM
  • function readData() {
        Office.context.document.getSelectedDataAsync(Office.CoercionType.Text, {}, function (result) {
            if (result.status == 'succeeded')
                tempData = result.value;
        });
    }

    This is returning a huge xml instead of just the content control ooxml.  Can you provide the least possible ooxml required to insert a simple plain text content control?

    Friday, September 28, 2012 12:53 AM
  • here is the smallest XML

    <?xml version="1.0" encoding="UTF-8" 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/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:sdt>

                           

    <w:sdtContent>

                               

    <w:p>

                                   

    <w:r>

                                       

    <w:t>This is Text</w:t>


                                   

    </w:r>


                               

    </w:p>


                           

    </w:sdtContent>


                       

    </w:sdt>


                   

    </w:body>


               

    </w:document>


           

    </pkg:xmlData>


       

    </pkg:part>

    </

    pkg:package>

    • Proposed as answer by JaysonGo Monday, August 5, 2013 6:00 PM
    Thursday, October 4, 2012 8:52 PM
  • Thecoercion type should be ooxml when calling getSelectedDataAsync. Otherwise, you're just going to get the placeholder text, and not the ooxmlthat represents the selected content control.

    Either way, your following answer with the "smallest ooxml" is absolutely correct. I was able to use it...thank you!

    Monday, August 5, 2013 1:34 PM