maurcz
maurcz

Reputation: 341

Error inserting text into word document

I have an MS Word add-in with code to insert text after a selection:

Word.run(function (context) {

    var selection = context.document.getSelection();           
    selection.insertText(text, Word.InsertLocation.after).select();

    return context.sync().then(function () {      
        console.log('Success');
    });

});

This code has been tested in many different machines without errors. However, to my surprise, one of our clients recently reported that no text at all is being added to his documents.

After some debugging, I've noticed that message Success is never printed to the console (context.sync().then is not being called). Just to be sure it isn't some crazy issue with the Office API, I ran this sample from office-js-docs directly on F12Chooser's console and had no issues:

// Run a batch operation against the Word JavaScript API.
Word.run(function (context) {

    // Create a proxy object for the document body.
    var body = context.document.body;

    // Queue a command to load the text property of the proxy body object.
    context.load(body, 'text');

    // Queue a command to insert text into the end of the Word document body.
    body.insertText('This is text inserted after loading the body.text property',
                    Word.InsertLocation.end);

    // Synchronize the document state by executing the queued commands,
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        console.log("Body contents: " + body.text);
    });
})

(text was insert at the end of the document)

Again, my code is working in a lot of different machines and setups, it seems to be an issue only in this person's machine. He's using:

Office Professional Plus 2016
Microsoft Word 2016 MSO (16.0.4266.1001) 64 Bits
Windows 10 Pro
Internet Explorer 11.413.15063

Could any one help me with this? Thanks!

Upvotes: 0

Views: 594

Answers (1)

Juan Balmori
Juan Balmori

Reputation: 5036

The reason why that code does not work in that specific box is because it’s an Office 2016 MSI SKU(builds 42xx) (not an Office365 aka Click-to-run sku) . MSI SKU has a few bugs associated with valid insertLocations and most of those bugs were fixed in the 1.2 release of the API. The bad news is that 1.2+ releases where not backported to MSI builds.

Please try insertLocation.end (instead of InsertLocation.after) and I think your code should work (with a slight different behavior)

Or if your customer moves to O365 the issue will be solved immediately.

Upvotes: 1

Related Questions