none
[API JavaScript for Word] How can I do page setup? RRS feed

  • Question

  • How can i set page style margins with new Java script api for microsoft office inside word taskpane addin?

    In VSTO addin i can setup page margins like:

    Document myDoc;

    myDoc.PageSetup.LeftMargin = 17;

    myDoc.PageSetup.RightMargin = 17;

    myDoc.PageSetup.TopMargin = 12;

    myDoc.PageSetup.BottomMargin = 14;


    Can i do the same with Java script API?

    Saturday, December 5, 2015 1:14 AM

Answers

  • Hi VSTOOffice,

    >> How can i set page style margins with new Java script api for microsoft office inside word taskpane addin?

    It is impossible. Office Add-in (Javascript for Office) is used to interop with data in Office, I have checked the supported APIs, and there is no such a method to set page setup.

    If this is a feature you want to include in future versions of app for Office, please submit a feedback to Office Development Platform Uservoice.
    http://officespdev.uservoice.com

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, December 7, 2015 2:41 AM
    Moderator

All replies

  • Hi VSTOOffice,

    >> How can i set page style margins with new Java script api for microsoft office inside word taskpane addin?

    It is impossible. Office Add-in (Javascript for Office) is used to interop with data in Office, I have checked the supported APIs, and there is no such a method to set page setup.

    If this is a feature you want to include in future versions of app for Office, please submit a feedback to Office Development Platform Uservoice.
    http://officespdev.uservoice.com

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, December 7, 2015 2:41 AM
    Moderator
  • It could be possible using the JaveScript APIs for Word 2016 if you manipulate the OOXML.

    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Monday, December 7, 2015 6:07 PM
    Moderator
  • Hi Cindy,

    I have tried to use Office.context.document.setSelectedDataAsync with ooxml, but I failed to change the “w:pgMar” element. I tried the code below, I get ooxml after I set with ooxml, but the value “w:pgMar” did not change, but “Test” string has been replaced with “Hello”. I assume “w:pgMar” might be supported to set by using ooxml. Did you have any suggestions about this?

    function getWordOOXML()
    {
        Office.context.document.getSelectedDataAsync("ooxml", function (asyncResult) {
            var error = asyncResult.error;
            if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                app.showNotification(error.name + ": " + error.message);
            }
            else {
                // Get selected data.
                var dataValue = asyncResult.value;
                xmlString = asyncResult.value;
                app.showNotification('Selected data is ' + dataValue);
            }
        });
    }
        
    function setWordOOXML() {
        var tarXml=xmlString.replace('w:right="1800"', 'w:right="720"');
        var tarXml=xmlString.replace('Test', 'Hello');
        Office.context.document.setSelectedDataAsync(tarXml, { coercionType: "ooxml" }, function (asyncResult) {
            var error = asyncResult.error;
            if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                app.showNotification(error.name + ": " + error.message);
            }
            else {
                // Get selected data.
                var dataValue = asyncResult.value;
                app.showNotification('Selected data is ' + dataValue);
            }
        })
    }
    

    If there is something wrong in my test, please feel free to let me know.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, December 9, 2015 11:24 AM
    Moderator
  • Hi Edward

    Thanks for testing this - I hadn't the opportunity to actually try it, which is why I used the conditional "could" rather than "can".

    Seems this is one more thing that Word "caches" and doesn't allow changing via OOXML <sigh>

    (I recently ran into similar problems with changing Numbering properties.)


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Wednesday, December 9, 2015 6:24 PM
    Moderator
  • Hi Cindy,

    Thanks very much for your help.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, December 10, 2015 5:35 AM
    Moderator
  • Hi Cindy (and Edward)!

    Do you know if this issue has been resolved yet?

    Beeing able to do page setup using office.js seems vital to me.

    Kind Regards,

    Fred

    Thursday, August 9, 2018 7:03 AM
  • No change, as far as I can tell. Probably because it's not supported by Word On-line.

    There is, however, Application.createFromTemplate so that for new documents you could start out with the desired settings...


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Friday, August 10, 2018 12:14 PM
    Moderator