| View previous topic :: View next topic |
| Author |
Message |
pseudomino General User

Joined: 03 Nov 2011 Posts: 6
|
Posted: Thu Nov 03, 2011 3:35 am Post subject: [Solved] Batch edit on hyperlinks in a writer document |
|
|
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 |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Thu Nov 03, 2011 4:49 am Post subject: |
|
|
In Find and Replace dialog, check in Regular expressions and
Search for
| Code: | | http://[^#]*(#[^ \.]*) |
Replace with
|
|
| Back to top |
|
 |
pseudomino General User

Joined: 03 Nov 2011 Posts: 6
|
Posted: Thu Nov 03, 2011 6:14 am Post subject: |
|
|
| 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 |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Thu Nov 03, 2011 4:48 pm Post subject: |
|
|
| 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 |
|
 |
pseudomino General User

Joined: 03 Nov 2011 Posts: 6
|
Posted: Sat Nov 05, 2011 2:29 pm Post subject: |
|
|
Excuse me, I don't understand
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 |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1414 Location: France
|
Posted: Mon Nov 07, 2011 12:37 am Post subject: |
|
|
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 / OpenOffice.org 3.4.1 en-US + langpacks, MS-Windows XP Home SP3
This forum is unusable, use instead Apache OpenOffice forums |
|
| Back to top |
|
 |
pseudomino General User

Joined: 03 Nov 2011 Posts: 6
|
Posted: Mon Nov 07, 2011 6:15 am Post subject: |
|
|
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 ?  |
|
| Back to top |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1414 Location: France
|
Posted: Mon Nov 07, 2011 6:28 am Post subject: |
|
|
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 / OpenOffice.org 3.4.1 en-US + langpacks, MS-Windows XP Home SP3
This forum is unusable, use instead Apache OpenOffice forums |
|
| Back to top |
|
 |
pseudomino General User

Joined: 03 Nov 2011 Posts: 6
|
Posted: Mon Nov 07, 2011 7:02 am Post subject: |
|
|
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 |
|
 |
|