OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
671 stars 96 forks source link

RichApi.Error - throw ItemNotFound when call insertField with Word.FieldType.ta #4938

Open NataliaKravcheniaTR opened 1 day ago

NataliaKravcheniaTR commented 1 day ago

Calling insertField with Word.FieldType.ta throw RichApi.Error - ItemNotFound error.

{
    "stack": "RichApi.Error: ItemNotFound\n    at new n (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:25:269308)\n    at r.processRequestExecutorResponseMessage (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:25:334277)\n    at https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:25:332338",
    "message": "ItemNotFound",
    "name": "RichApi.Error",
    "code": "ItemNotFound",
    "traceMessages": [],
    "innerError": null,
    "debugInfo": {
        "code": "ItemNotFound",
        "message": "ItemNotFound",
        "errorLocation": "Field._OnAccess",
        "statement": "var insertField = range.insertField(...);",
        "surroundingStatements": [
            "var root = context.root;",
            "var selection = root.getSelection();",
            "// Instantiate {selection}",
            "var range = selection.getRange();",
            "// Instantiate {range}",
            "// >>>>>",
            "var insertField = range.insertField(...);",
            "// <<<<<",
            "// Instantiate {insertField}",
            "insertField.load([\"result\",\"code\"]);"
        ],
        "fullStatements": [
            "Please enable config.extendedErrorLogging to see full statements."
        ]
    },
    "httpStatusCode": 404
}

Your Environment

Expected behavior

TA field is inserted without error

Current behavior

TA field is inserted, but throw error

Steps to reproduce

Load this script in Script Lab:

name: Insert TA field error
description: ''
host: WORD
api_set: {}
script:
  content: |
    $("#run").on("click", () => tryCatch(insertTAField));

    async function insertTAField() {
      await Word.run(async (context) => {
        var range = context.document.getSelection().getRange();

        const field = range.insertField(
          Word.InsertLocation.after,
          Word.FieldType.ta,
          '\\l "Low with Miller" \\b \\c 8 \\i',
          true
        );

        field.load("result,code");
        await context.sync();

        if (field.isNullObject) {
          console.log("There are no fields in this document.");
        } else {
          console.log("Code of the field: " + field.code, "Result of the field: " + JSON.stringify(field.result));
        }
      });
    }

    // Default helper for invoking an action and handling errors.
    async function tryCatch(callback) {
      try {
        await callback();
      } catch (error) {
        // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
        console.error(error);
      }
    }
  language: typescript
template:
  content: |-
    <button id="run" class="ms-Button">
        <span class="ms-Button-label">Insert TA field</span>
    </button>
  language: html
style:
  content: |-
    section.samples {
        margin-top: 20px;
    }

    section.samples .ms-Button, section.setup .ms-Button {
        display: block;
        margin-bottom: 5px;
        margin-left: 20px;
        min-width: 80px;
    }
  language: css
libraries: |
  https://appsforoffice.microsoft.com/lib/1/hosted/office.js
  @types/office-js

  office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
  office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css

  core-js@2.4.1/client/core.min.js
  @types/core-js

  jquery@3.1.1
  @types/jquery@3.3.1
microsoft-github-policy-service[bot] commented 1 day ago

Thank you for letting us know about this issue. We will take a look shortly. Thanks.