I’m working on automating some tasks in a Word document using Macro. The current documentation mostly covers adding hyperlinks to external sources (like websites), but I want to create a hyperlink that jumps to a specific bookmark.
Has anyone done this before? Is there a specific method or syntax for linking to internal bookmarks via macro?
Thanks in advance!
Document Server version: 8.2
Hello @Vipin896
Let me verify this possibility. I’ll come back with an update as soon as possible.
I was informed that there is currently no way to add hyperlinks to the parts of the document. An enhancement has been registered to implement such possibility. I’ll update the topic once any news comes up.
@Constantine Thanks a lot.
Please update here if this feature got accommodate in enhancement
1 Like
I will let you know once it becomes available.
+1 on a similar use case : a hyperlink jumping to the internal document headings
To achieve this, is possible to use the API to search a heading starting with the selected text, add a bookmark next to the matching heading and use AddBookmarkCrossRef method to create an internal link, but if you want multiple links to the same heading, you need to create a new bookmark at the same position everytime, and the hyperlink style (underlined blue) is not applied so that you don’t know the link exist until you hover the text.
Here is a plugin js code doing this :
(function(window, undefined){
window.Asc.plugin.init = function() {
this.callCommand(function() {
let doc = Api.GetDocument();
let range = doc.GetRangeBySelect();
// Get all paragraphs in selection
let paras = range.GetAllParagraphs(); // ApiParagraph[]
if (!range) {
console.log("No selection detected.");
return;
}
let para = paras[0];
let selectedText = range.GetText().trim(); //
if (!selectedText) {
console.log("The selected text is empty.");
return;
}
// Search for a corresponding heading (Heading 1..6)
let headings = doc.GetAllHeadingParagraphs();
let found = null;
for (let i = 0; i < headings.length; i++) {
let h = headings[i];
let hText = h.GetText().trim();
if (hText.toLowerCase().startsWith(selectedText.toLowerCase())) {
found = h;
break;
}
}
if (!found) {
console.log('No heading starting with "' + selectedText + '" was found.');
return;
}
// Create a bookmark on found heading
let bookmarkName = "bm_" + Date.now();
console.log("Bookmark créé " + bookmarkName)
let hRange = found.GetRange();
if (!hRange.AddBookmark(bookmarkName)) {
console.log("Error: cannot create bookmark");
return;
}
// Add the cross-reference on selected text
range.Delete();
let ok = para.AddBookmarkCrossRef("text", bookmarkName, true, false, selectedText);
console.log("Cross-reference successfully inserted to the heading : " + found.GetText());
}, true);
};
window.Asc.plugin.button = function(id){};
})(window, undefined);
I’m not a developer, so maybe a better solution already exists but I think using the AddHyperlink function to make it work like Right click → Add hyperlink → Place in document would be much smoother
Hello @FreeOff
That is exactly what original poster suggests. I will forward your query to the existing bug.