[Writer] Use another style for links [closed]
My document contains two kinds of hyperlinks:
- Links to bookmarks (in the same document)
- Links to web pages
I want these links to display differently. Since they are considered the same (why? we could have had a nice hierarchy of link styles), I want to define a new style, and I will have to apply it manually to every bookmark link. I called this style "Reference":
This way I will have web links with the default blue, and bookmark links with the cute dotted underline. This means I don't want to edit the default style. I want to apply a different style to some of the links.
So I double-clicked on my "Reference" style and LO says the style has been applied. But however I look, nothing changed.
.
How can I apply the custom style to my internal links?
EDIT The problem has been solved by resetting the default Internet link
style to Standard
(see @ajlittoz 's answer), and creating two new styles : DocLink
(black + dotted underline) and WebLink
(blue + single underline).
Following Jim K's advice, I also wrote a macro to automate the application of those styles. This is my first time writing a macro so it took me a little while to use correctly Basic and the LibreOffice API. Full source code (I don't know how to use syntax highlighting):
' Append an item to an Object array '
Private Sub append(array() as Object, item as Object)
ReDim Preserve array(UBound(array) + 1)
array(UBound(array)) = item
End Sub
' Find all links in the given document fragment '
' From: https://stackoverflow.com/questions/37611030 '
Private Sub findLinks(fragment as Object, links as Object)
Dim portions as Object
Dim portion as Object
Select Case fragment.implementationName
Case "SwXParagraph": ' Paragraphs are enumerable '
Dim msg as String
portions = fragment.createEnumeration
While portions.hasMoreElements
portion = portions.nextElement
If Len(portion.HyperLinkURL) > 0 Then
' Ignore internal LibreOffice links (#__) '
If Left(portion.HyperLinkURL, 3) <> "#__" Then
append(links, portion)
End If
End If
Wend
Case "SwXTextTable": ' Tables have cells '
Dim cells as Object
Dim cell as Object
Set cells = fragment.getCellNames()
For i = LBound(cells) to UBound(cells)
Set cell = fragment.getCellByName(cells(i))
Set portions = cell.createEnumeration()
While portions.hasMoreElements
findLinks(portions.nextElement, links)
Wend
Next
End Select
End Sub
' Find all links (document & internet links) in the document '
' and apply a custom style depending on the link type '
Sub AutoLinkStyle
' I suppose in Writer "ThisComponent" is an instance of '
' "com.sun.star.text.TextDocument". Is ".Text" an alias '
' for ".getText()"? I couldnt find docs explaining it '
Dim document as Object
Dim fragments as Object
Dim links() as Object
Set document = ThisComponent
Set fragments = document.Text.createEnumeration()
' Get a list of all links in the document '
While fragments.hasMoreElements
findLinks(fragments.nextElement, links)
Wend
' Apply custom styles '
Dim msg as String
For i = LBound(links) to UBound(links)
If Left(links(i).HyperLinkURL, 1) = "#" Then
links(i).CharStyleName = "DocLink"
Else
links(i).CharStyleName = "WebLink"
End If
Next
End Sub
Did you forget to select the word(s) which are part of your link before applying your character style?
@ajlittoz Nope, I even spent minutes re-applying said style over and over just to make sure. The "Styles and Formatting" panel highlights correctly my style when I select the link. When I put the cursor right after the link and write some more, the new text (not part of the link) has the right custom style. But it is never shown on the links.
This is a follow-up question from https://askubuntu.com/questions/99134....
Good job writing the macro. For syntax highlighting on this site, single quotes
'
must match, so add a'
to the end of each commented line as well as at the beginning.