1

In my Word Add-In, I want my user to be able to insert a template, with editable content controls. Works fine in Word client, but in Word online, I can't get it to work properly.

You'll find my code below. I get a debug error saying The action isn't supported in Word Online. Error location : ContentControl.placeholderText. Indeed when I comment out the line setting placeholderText, I get no error. But then my add-in only adds lines of empty text, which is not my purpose obviously.

Word.run(function (context) {
        var thisDocument = context.document,
            range = thisDocument.getSelection();

            case "ProtagonistsIntroduction":
                var paragraph = range.insertParagraph("Customer", Word.InsertLocation.before);
                paragraph.styleBuiltIn = "Heading1";
                setTemplateContentControlProperties(range, "Mr/Ms. and name", true);
                //I insert here many other content controls
                break;
    })
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });
function setTemplateContentControlProperties(container, placeholderText, lineBreakAfter) {
        var contentControl = container.insertContentControl();
        contentControl.appearance = "BoundingBox";
        contentControl.removeWhenEdited = true;
        contentControl.placeholderText = placeholderText;
        if (lineBreakAfter) {
            contentControl.insertBreak("Line", Word.InsertLocation.after);
        }
        return contentControl;
    }

As this tool is here to provide the user with a document template with all the information that he should include, I need these placeholders. Is there a way around this ?

I would be OK with a way of just adding lines of text instead of content controls when in Word Online ; but adding the content controls when I'm in a compatible environment (Word client) ; if there isn't anything better than that.

Cheers !

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63

1 Answers1

2

The inability to set placeholderText in Word Online is a known limitation of the Word API, as described in this GitHub issue: https://github.com/OfficeDev/office-js/issues/53. I'd suggest that you subscribe to notifications for that GitHub issue to be notified of any updates there from the product team.

Kim Brandl
  • 13,125
  • 2
  • 16
  • 21
  • Ok thanks. Can I detect that I am in Word Online then, to proceed with a different method ? – Marius Conjeaud Apr 10 '18 at 15:02
  • 1
    There are "helper APIs" that enable you to detect host and platform info -- see [this SO answer](https://stackoverflow.com/questions/40688057/in-excel-online-officejs-api-is-not-passing-the-host-info-parameter-anymore-to/40963500#40963500) for more info about that topic. – Kim Brandl Apr 10 '18 at 15:14
  • Thanks I'll check that out ! – Marius Conjeaud Apr 11 '18 at 07:02