OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[Solved] Batch edit on hyperlinks in a writer document

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
pseudomino
General User
General User


Joined: 03 Nov 2011
Posts: 6

PostPosted: Thu Nov 03, 2011 3:35 am    Post subject: [Solved] Batch edit on hyperlinks in a writer document Reply with quote

Hello,

I have a huge number of hyperlinks in a odt file, and I have to clean them this way :
http://www.mysite.com/page.php#anchor1 > #anchor1
http://www.mysite.com/page.php#anchor2 > #anchor2
etc...

This must be very easy, but I never made any macro with OOo.
Your help would be welcome !

Thx


Last edited by pseudomino on Mon Nov 07, 2011 7:03 am; edited 1 time in total
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Thu Nov 03, 2011 4:49 am    Post subject: Reply with quote

In Find and Replace dialog, check in Regular expressions and
Search for
Code:
http://[^#]*(#[^ \.]*)

Replace with
Code:
$1
Back to top
View user's profile Send private message
pseudomino
General User
General User


Joined: 03 Nov 2011
Posts: 6

PostPosted: Thu Nov 03, 2011 6:14 am    Post subject: Reply with quote

Thanks but the "http://www.mysite.com/page.php#anchor1" is in the link dialog, not in the text. So the search and replace tool can't find it...
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Thu Nov 03, 2011 4:48 pm    Post subject: Reply with quote

Your problem is unclear for me. The link dialog is not accessible by normal ways, it should be done by the way to use com.sun.star.awt.XTopwindowListener on the toolkit.
Back to top
View user's profile Send private message
pseudomino
General User
General User


Joined: 03 Nov 2011
Posts: 6

PostPosted: Sat Nov 05, 2011 2:29 pm    Post subject: Reply with quote

Excuse me, I don't understand Embarassed

Using Word, I can achieve this simple task with this VBA script (because seemingly the "#anchor" part doesn't belong to "Adress") :
Code:
For Each OneHyperlink In WordDoc.Hyperlinks
OneHyperlink.Address = " "
Next OneHyperlink


but I would like to be able to do that with OOo
Back to top
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Mon Nov 07, 2011 12:37 am    Post subject: Reply with quote

Hi,
pseudomino wrote:
This must be very easy, but I never made any macro with OOo. (...)
Using Word, I can achieve this simple task with this VBA script (...)

OpenOffice programming is not VBA programming.
You will have to learn OpenOffice.org API.

There is no simple way (AFAIK) of obtaining by program the hyperlinks in a Writer document, although the Navigator of OpenOffice can do this.
In the simplest case, if your hyperlinks are all in the paragraphs of the main text of the document, there is a solution in this thread.
But hyperlinks can be anywhere else : in a cell of a table, in a frame, in a header or footer, in a note, etc, or a combination of those. There is no general solution to all cases, but specific solutions to particular cases.
_________________
Bernard

OpenOffice.org 1.1.5 fr / Apache OpenOffice 4.0.1 / LibreOffice 4.1.0
MS-Windows 7 Home SP1
This forum is spammed, use instead Apache OpenOffice forums
Back to top
View user's profile Send private message Visit poster's website
pseudomino
General User
General User


Joined: 03 Nov 2011
Posts: 6

PostPosted: Mon Nov 07, 2011 6:15 am    Post subject: Reply with quote

I know OOo Basic is not VBA, that's the reason of this thread.

Anyway, thanks for the link to this code, which helps me a lot, since the links are only in the "main text" in the files I'm working on.

Code:
Sub EditHyperlinks 'List, unlink or delete.
Dim oDoc,oEnum,oEnum1,oPara,oPortion,s
oDoc = ThisComponent
oEnum = oDoc.Text.createEnumeration
While oEnum.hasMoreElements
 oPara = oEnum.nextElement
 oEnum1 = oPara.createEnumeration
 While oEnum1.hasMoreElements
  oPortion = oEnum1.nextElement
  If oPortion.HyperLinkURL <> "" then
   oPortion.HyperLinkURL = ""
  EndIf
 Wend
Wend
End Sub


And the code is quite understandable (the only point I don't understand is why there is a double Enumeration)

But contrary to VBA, HyperLinkURL property contains "all the link path", including "#anchor". So I guess I have to use some regex.
Unfortunately, I read on some threads that OOoBasic doesn't have built-in regex ?

There again, I would be able to do that using VBA this way :

Code:
Function CleanLink(link As String) As String
   Dim reg As VBScript_RegExp_55.RegExp
   
   Set reg = New VBScript_RegExp_55.RegExp
   reg.Pattern = "http://[^#]*(#[^ \.]*)"
   
   If reg.test(link) Then
      CleanLink = reg.Replace(link, "$1")
   Else
      CleanLink = link
   End If
End Function


Do you think I can I go further or is this the end of my attempt ? Sad
Back to top
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Mon Nov 07, 2011 6:28 am    Post subject: Reply with quote

Why use such complex thing as regex ?
Code:
Function CleanLink(link As String) As String
  Dim p As Long
  p = InStr(1, link, "#", 0)
  CleanLink = Mid(link, p)
End Function

_________________
Bernard

OpenOffice.org 1.1.5 fr / Apache OpenOffice 4.0.1 / LibreOffice 4.1.0
MS-Windows 7 Home SP1
This forum is spammed, use instead Apache OpenOffice forums
Back to top
View user's profile Send private message Visit poster's website
pseudomino
General User
General User


Joined: 03 Nov 2011
Posts: 6

PostPosted: Mon Nov 07, 2011 7:02 am    Post subject: Reply with quote

Thanks a lot ! That's just perfect !


A in nutshell :

Code:
Sub CleanHyperlinks
Dim oDoc,oEnum,oEnum1,oPara,oPortion,s
Dim p As Long
oDoc = ThisComponent
oEnum = oDoc.Text.createEnumeration
While oEnum.hasMoreElements
 oPara = oEnum.nextElement
 oEnum1 = oPara.createEnumeration
 While oEnum1.hasMoreElements
  oPortion = oEnum1.nextElement
  If oPortion.HyperLinkURL <> "" then
   p = InStr(1, oPortion.HyperLinkURL, "#", 0)
   oPortion.HyperLinkURL = Mid(oPortion.HyperLinkURL, p)
  EndIf
 Wend
Wend
End Sub
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group