Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When set hyperlink to range with field remove text #5341

Open
NataliaKravcheniaTR opened this issue Jan 31, 2025 · 2 comments
Open

When set hyperlink to range with field remove text #5341

NataliaKravcheniaTR opened this issue Jan 31, 2025 · 2 comments
Assignees
Labels
Area: Word Issue related to Word add-ins Platform: Desktop

Comments

@NataliaKravcheniaTR
Copy link

When set hyperlink to range with TA field, it removes visible text

Your Environment

  • Platform: PC desktop
  • Host: Word

Expected behavior

Link will be inserted in text

Current behavior

Link inserted in hidden text, visible text is removed/hidden
Hide hidden text:
Image
Show hidden text:
Image

Steps to reproduce

Add some text, add citation at the beginning, add bookmark (bookmark - id) which includes TA tag and text.

Image

Load this script in Script Lab:

name: Set hyperlink to range with TA field
description: ''
host: WORD
api_set: {}
script:
  content: |
    $("#run").on("click", () => tryCatch(insertHyperlink));

    async function insertHyperlink() {
      await Word.run(async (context) => {
        const bookmarkRange = context.document.getBookmarkRange("bookmark");

        await context.sync();

        bookmarkRange.hyperlink = "test.com";

        await context.sync();
      });
    }

    // 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 hyperlink to bookmark</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

  [email protected]/dist/css/fabric.min.css
  [email protected]/dist/css/fabric.components.min.css

  [email protected]/client/core.min.js
  @types/core-js

  [email protected]
  @types/[email protected]

I've found a workaround for this issue

async function insertHyperlink() {
  await Word.run(async (context) => {
    const bookmarkRange = context.document.getBookmarkRange('bookmark')

    bookmarkRange.load('fields')
    await context.sync()
    const field = bookmarkRange.fields.items[0]

    field.select('End')

    await context.sync()
    const selection = context.document.getSelection()

    await context.sync()

    const range = selection.expandTo(bookmarkRange.getRange('End'))
    range.hyperlink = "test.com"
    await context.sync()
  });
}
@microsoft-github-policy-service microsoft-github-policy-service bot added the Area: Word Issue related to Word add-ins label Jan 31, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: attention 👋 Waiting on Microsoft to provide feedback label Jan 31, 2025
Copy link
Contributor

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

@guoms1 guoms1 added Platform: Desktop and removed Needs: attention 👋 Waiting on Microsoft to provide feedback labels Feb 6, 2025
@guoms1
Copy link

guoms1 commented Feb 6, 2025

Hi, @NataliaKravcheniaTR

Thank you for bringing this to our attention! I've been able to reproduce the issue on my end and will be forwarding it (#9761210) to the relevant team for further investigation.

We truly appreciate your patience and your effort in reporting this. Please let us know if you have any additional details that might help.

Best,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Word Issue related to Word add-ins Platform: Desktop
Projects
None yet
Development

No branches or pull requests

2 participants