l p
l p

Reputation: 528

Is there official clipboard access from office-js for Word add-in?

I have reviewed the office-js docs and not found any formal way of working with the clipboard through a Word add-in.

I attempted to use the newer navigator.clipboard.writeText but it is automatically rejected by the internal Word browser (where the add-in is hosted) without prompting the user (like a browser does). I have not seen any associated support for permissions policy that can be used to communicate with Word that the add-in would like clipboard permissions.

So, I am resorting to the deprecated document.executeCommand('copy') for now, but am concerned that will get my add-in rejected by the MS App Store.

Is there an official/sanctioned way to use the clipboard from within a MS Word add-in?

Upvotes: 1

Views: 892

Answers (4)

Michael Dausmann
Michael Dausmann

Reputation: 4540

This is super irritating. See related ticket (https://github.com/OfficeDev/office-js/issues/1991) I have had some success with the following workaround. Seems to work in my current version of chrome - 118.0.5993.70 (Official Build) (arm64)

export function navigator_clipboard_writeText(text: string): Promise<void> {
  return new Promise(function (resolve, reject) {
    try {
      const textArea = document.createElement('textarea');
      textArea.value = text;
      document.body.appendChild(textArea);
      textArea.select();
      document.execCommand('copy');
      textArea.remove();
      resolve();
    } catch (e) {
      reject(e);
    }
  });
}

Upvotes: 0

justmars
justmars

Reputation: 21

The restriction resulting in the console error:

The Clipboard API has been blocked because of a permissions policy applied to the current document...

is limited to OfficeJS invoked via the web browser but if invoked via a client desktop device like Word on MacOS, the navigator.clipboard.writeText appears to work.

Upvotes: 0

Xurui Yao
Xurui Yao

Reputation: 36

It seems like there's one method Range.copyFrom of Excel rich client api. I can't find the alternatives either. Maybe you can ask to provide the new feature. Whether it could be in the future feature list or not may also take some time.

Upvotes: 1

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

OfficeJS doesn't provide anything for that. You may sill use the Document.execCommand() API which includes "copy", "cut" and "paste". But it was deprecated and may not work in the web browsers any longer.

Feature requests on Tech Community are considered, when the dev team go through the planning process. Use the github label: Type: product feature request at https://aka.ms/M365dev-suggestions .

Upvotes: 1

Related Questions