Ask Your Question

Change target frame to _blank for all hyperlinks in Libreoffice Writer

asked 2019-02-22 15:22:44 +0200

As an editor, I receive files in the most different formats. Before publishing articles on our CMS, I usually remove all formatting (ctrl+M). I also need to turn all hyperlinks into links with "_blank" as their target frame.

This can be done one by one, but it is tedious.

If I select all text, and then Format -> Character -> Hyperlink, I can select blank as Target frame. But if I remove all formatting, links also disappear, which makes it useless.

Maybe there is some way to go around it with the AltSearch extension, but I was not able to get it to do what I wanted.

Any suggestions?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2019-02-23 00:36:44 +0200

Lupp gravatar image

updated 2019-02-23 00:40:12 +0200

I think you will need user code for the purpose.
If your big document does not contain tables and there are no TextFrames or only TextFrames not containing hyperlinked text, it is rather simple to search for all the hyperlinks, and to change them as you think to need ("_blank" target frame).
If TextTables occur, it's significantly more complicated.
The code:

Sub clearFormattingAndDirectHyperlinksToBlank()
REM This roughly sketched macro CAN NOT HANDLE text inside TextFrames.
REM It will fail and throw an error if the body text is containing TextTables.

theDoc       = ThisComponent
currCtrl     = theDoc.CurrentController
oldSel       = theDoc.CurrentSelection
dispProvider = currCtrl.Frame
dispHelper   = createUnoService("")
dispHelper.executeDispatch(dispProvider, ".uno:SelectAll", "", 0, Array())
dispHelper.executeDispatch(dispProvider, ".uno:ResetAttributes", "", 0, Array())
bText        = theDoc.Text
For Each para in bText
  para.ParaStyleName = "Default Style"
  For Each piece in para
    url = urlIf(piece)
    If url<>"" Then
      piece.HyperLinkTarget = "_blank"
    End If
  Next piece
Next para
End Sub

 Function urlIf(pPiece As Object) As String
 On Local Error Goto errorExit  
 urlIf = "" 
 urlIf = pPiece.HyperLinkURL 
 End Function

And a demo.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-02-22 15:22:44 +0200

Seen: 23 times

Last updated: Feb 23