Hello @leeand00,
Assuming that the Hyperlinks’ internal targets are also defined inside your pasted document (under Bookmarks), it might be easier to change the Hyperlink’s target URL and set it to the jumpmark corresponding to the internal target, instead of converting them all into References.
For example if the base URL is “https://www.site.org/page” and the target URL is “https://www.site.org/page#section”, then you could just change the target URL into “#section” to make it jump internally.
I made a quick macro that does this automatically, but currently it only works in those cases where the given base URL string is exactly the same as the part before the “#” in the target URL.
Please try if the macro works already for your pages, else i’ll adjust the macro.
Sub Writer_Internalize_Hyperlinks( strBaseURL as String )
REM https://ask.libreoffice.org/t/convert-anchor-hyperlinks-to-references-automatically/29283
REM After importing a webpage into Writer, it might contain Hyperlinks that are supposed to jump to another section on the page,
REM but instead they jump to that section on the internet web page itself.
REM This method sets the Target URL of such Hyperlinks to the corresponding Bookmark in Writer.
REM <strBaseURL> : full URL String of the page that contains the internal Hyperlinks, e.g. "https://www.site.org/page"
Dim oDoc as Object : oDoc = ThisComponent
Dim oText as Object : oText = oDoc.getText()
Dim oParagraphs as Object : oParagraphs = oText.createEnumeration()
Dim oParagraph as Object
Dim oTextPortions as Object
Dim oTextPortion as Object
Dim strURL as String
Do While oParagraphs.hasMoreElements()
oParagraph = oParagraphs.nextElement
If oParagraph.supportsService( "com.sun.star.text.Paragraph" ) Then
oTextPortions = oParagraph.createEnumeration()
Do While oTextPortions.hasMoreElements()
oTextPortion = oTextPortions.nextElement()
strURL = oTextPortion.HyperlinkURL
If strURL <> "" Then REM it's a Hyperlink!'
If Left( strURL, Len( strBaseURL ) ) = strBaseURL Then REM Only works if both URL formats are the same.
oTextPortion.HyperlinkURL = Mid( strURL, Len( strBaseURL ) + 1 ) REM keep jumpmark.
End If
End If
Loop
End If
Loop
End Sub