# [Writer] Use another style for links [closed]

My document contains two kinds of hyperlinks:

• Links to bookmarks (in the same document)

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 '
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
' Ignore internal LibreOffice links (#__) '
If Left(portion.HyperLinkURL, 3) <> "#__" Then
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
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 '
' 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
Set document = ThisComponent
Set fragments = document.Text.createEnumeration()

' Get a list of all links in the document '
While fragments.hasMoreElements
Wend

' Apply custom styles '
Dim msg as String
Else
End If
Next
End Sub

edit retag reopen merge delete

### Closed for the following reason the question is answered, right answer was accepted by gnxr close date 2018-01-07 16:31:38.100224

Did you forget to select the word(s) which are part of your link before applying your character style?

( 2018-01-02 23:25:34 +0200 )edit

@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.

( 2018-01-03 00:05:47 +0200 )edit

This is a follow-up question from https://askubuntu.com/questions/99134....

( 2018-01-03 00:46:24 +0200 )edit

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.

( 2018-01-08 20:08:06 +0200 )edit

Sort by » oldest newest most voted

From my experiments, Internet Link does not behave as an ordinary character style: it seems to be an "internal" style which does not preclude others. As I understand it (I'm no developper and have not read yet any line of LO code), the style layers here are: character style, covered by Internet Link, partially covered by direct formatting (due to the "partial" effect, the correct ordering may be in fact: character style, direct formatting, Internet Link with standard overriding rules).

As an exception to standard behaviour, two character styles can be assigned to a link: a user one and Internet Link. The latter is a consequence (or the cause) of the link property and can only be removed by right-click and Remove Hyperlink.

If you need the hyperlink feature, you can't remove Internet Link but you can make profit of the double character style possibility to achieve different link appearance.

First, get Internet Link out of interference by giving it a "neutral" presentation:

• Open Internet Link for modification
• In tab Font Effects, push the Standard button; this will reset all attributes to the state of Default Style, i.e. no change to current paragraph style.
• You can do the same if you want to override attributes in other tabs.
• OK to save your new defintion.

Apply the desired custom character style to the link. It retains the link property from Internet Link and displays graphical attributes from your character style.

I have not played with Visited Internet Link but the same trick should do.

I don't know if this is a hack and will persist across updates to code base.

@Jim K: you are not restricted to underline + blue color for links; it's just that Internet Link properties override those in the custom style until you reset attributes

If this answer helped you, please accept it by clicking the check mark ✔ to the left and, karma permitting, upvote it. If this resolves your problem, close the question, that will help other people with the same question.

more

Good point about the Standard button, which is much better than unzipping the file as suggested in my answer. Other than that, it sounds like we came to the same conclusion.

( 2018-01-03 18:40:25 +0200 )edit
1

Standard is not well described, if at all, and its importance/usefulness is highly underestimated. It saved my day more than once.

( 2018-01-04 08:43:59 +0200 )edit

Standard is exactly what I was looking for. Thanks!

( 2018-01-07 16:34:26 +0200 )edit

Apparently, trying to use a different hyperlink style that does not have underlines or use blue color does not work. It will not override the "Internet Link" style for these attributes. DIrect formatting can override the style, but you are right to avoid direct formatting.

So let's do it the opposite way. Modify "Internet Link" and "Visited Internet Link" to look like "Default Style". This sounds easy, but it's actually the hard part. That's because removing the underline from within LibreOffice specifies "without underline" rather than leaving it unspecified. So instead, I unzipped the .odt file and edited styles.xml to remove these attributes from "Internet_20_link" and "Visited_20_Internet_20_link".

Now, create a new character style called "Underlined Link" to be used for internet hyperlinks. Set it to underlined and blue, and then find each internet link and set them to use this style. The result: non-underlined links to bookmarks.odt

Regarding the other part of the question, with this approach, it will be necessary to format each internet hyperlink to use the "Underlined Link" style. If there are a lot of them, you could automate this with a macro that sets the styles based on the HyperLinkURL property.

more

I have lots of web links and bookmarks so I ended up writing a macro following your advice (see my updated question). Thanks!

( 2018-01-07 16:39:04 +0200 )edit

For Writer, bookmarks and web links are both "hyperlinks" and that's why they get the same character style. There is no way around to automate this, AFAIK.

Your only option is to not use bookmarks at all but cross references instead. Then to the cross reference marker you can apply any character style.

EDIT: as @ajlittoz says, you should be able to manually overwrite the character style, though.

more