Help with macro adding outline hyperlinks in Writer

I am having trouble with a macro that adds Hyperlinks. Here is a basic example of adding a link.

	dim args1(4) as new com.sun.star.beans.PropertyValue
	args1(0).Name = "Hyperlink.Text"
	args1(0).Value = "Dentistry"
	args1(1).Name = "Hyperlink.URL"
	args1(1).Value = "#11.20.46.Dentistry|outline"
	args1(2).Name = "Hyperlink.Target"
	args1(2).Value = ""
	args1(3).Name = "Hyperlink.Name"
	args1(3).Value = ""
	args1(4).Name = "Hyperlink.Type"
	args1(4).Value = 1
	
	dispatcher.executeDispatch(document, ".uno:SetHyperlink", "", 0, args1())

That’s simple enough but I need to scan the document’s outline and pull out certain links and use them in arg1(1).value.

How do I do that in a Basic script? btw using LO 7.1 on Ubuntu

Are you creating a link to a bookmark in the document itself or an external web hyperlink (URL)?

It’s a link within the doc, that’s why I need a way to scan the doc’s outline.

Look at XLinkTargetSupplier interface.

To find bookmarks in the document and get the target and the target text,

	Dim oDoc: oDoc = ThisComponent
	Dim obookmarks as object: obookmarks = oDoc.Bookmarks
	Dim i,obookmark, t, n
	for i = 0 to obookmarks.count -1
		obookmark = obookmarks(i)
		'Bookmark name 
		n = obookmark.Name
		
		'Bookmark text in the document
		t =  obookmark.anchor.getString()
		
		print n & " : " & t
	next

so then I would check the value of getString, if it matches assign args1(1).Value = the value of obookmark.Name adding the prefix #

HTH

Maybe the insertion via API could help

rem source: https://forum.openoffice.org/en/forum/viewtopic.php?p=504974&sid=a3ada540393963ef6ca5db0d8a860f60#p504974
Sub insertHyperlinkToDoc()
	dim oDoc as object, oText as object, sUrl1$, sUrl2$, oPos1 as object, oPos2 as object
	sUrl1="https://ask.libreoffice.org/en/questions/"
	sUrl2="https://libreoffice.org/"
	oDoc=ThisComponent
	oText=oDoc.Text
	oText.String=" Wow " 'insert some text
	
	oPos1=oText.Start 'start of inserted text
	with oPos1 'hyperlink1
		.String=sUrl1
		.HyperlinkURL=sUrl1
		.CharStyleName="Internet link"
	end with
	
	oPos2=oText.End 'end of inserted text
	with oPos2 'hyperlink2
		.String=sUrl2
		.HyperlinkURL=sUrl2
		.CharStyleName="Internet link"
	end with
End Sub