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

code to list all hyperlinks in a OOo document

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


Joined: 12 Feb 2008
Posts: 20

PostPosted: Fri Feb 29, 2008 4:04 am    Post subject: code to list all hyperlinks in a OOo document Reply with quote

I need code to list all hyperlinks in my OOo Writer document, I have found code here http://www.oooforum.org/forum/viewtopic.phtml?t=9957 that adds a hyperlink to text,
Back to top
View user's profile Send private message
jwesonga
General User
General User


Joined: 12 Feb 2008
Posts: 20

PostPosted: Fri Feb 29, 2008 5:04 am    Post subject: count all hyperlinks Reply with quote

I took the code for searching and replacing hyperlinks http://www.oooforum.org/forum/viewtopic.phtml?t=46675 and ammended it so that I could count the hyperlinks:

Code:
REM  *****  BASIC  *****
dim objEnumPar as object, objText as object, oDoc as object

Sub Main
initOOo
ListHyperLinks thisComponent
End Sub

Sub initOOo
oDoc = thisComponent'starDesktop.currentComponent
objText=oDoc.GetText

End Sub

Sub ListHyperlinks (oDoc )
   nCount  = 0
   objEnumPar=objText.createEnumeration

   Do While objEnumPar.hasMoreElements()
      objPar = objEnumPar.nextElement()
      objEnumChunks = objPar.createEnumeration()
      
      Do While objEnumChunks.hasMoreElements()
         objChunk = objEnumChunks.nextElement()
         XRay objChunk
         If objChunk.HyperLinkURL <> "" Then
                   nCount  = nCount + 1
              End If
         
      Loop
   Loop
   msgbox "Number of hyperlinks " & nCount
End Sub
Back to top
View user's profile Send private message
probe1
Moderator
Moderator


Joined: 18 Aug 2004
Posts: 2465
Location: Chonburi Thailand Asia

PostPosted: Fri Feb 29, 2008 5:14 am    Post subject: Reply with quote

... this code won't get you hyperlinks in other areas than "Text": tables, headers, footers, frames, ...
_________________
Cheers
Winfried
My Macros
DateTime2 extension: insert date, time or timestamp, formatted to your needs
Back to top
View user's profile Send private message Visit poster's website
jwesonga
General User
General User


Joined: 12 Feb 2008
Posts: 20

PostPosted: Fri Feb 29, 2008 6:44 am    Post subject: Reply with quote

probe1 wrote:
... this code won't get you hyperlinks in other areas than "Text": tables, headers, footers, frames, ...


For now all I need is hyperlinks within the text so for now its okay. The code only counts the items, how can I get the properties of the hyperlinks that have been identified? In the form of:

Property- Value
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 8979
Location: Lexinton, Kentucky, USA

PostPosted: Fri Feb 29, 2008 7:14 am    Post subject: Reply with quote

Taking that code as a starting point and, as indicated, only looking in normal text, you could use this.
Code:
Sub GetHyperlinks '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
   REM list it
   s = s & "URL = " & oPortion.HyperLinkURL & " - Text = " & oPortion.String & Chr(13)
   REM unlink it
   'oPortion.HyperLinkURL = ""
   REM delete it
   'oPortion.String = ""
  EndIf
 Wend
Wend
REM show list
MsgBox s   
End Sub
Back to top
View user's profile Send private message
jwesonga
General User
General User


Joined: 12 Feb 2008
Posts: 20

PostPosted: Fri Feb 29, 2008 7:36 am    Post subject: Reply with quote

JohnV wrote:
Taking that code as a starting point and, as indicated, only looking in normal text, you could use this.
Code:
Sub GetHyperlinks '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
   REM list it
   s = s & "URL = " & oPortion.HyperLinkURL & " - Text = " & oPortion.String & Chr(13)
   REM unlink it
   'oPortion.HyperLinkURL = ""
   REM delete it
   'oPortion.String = ""
  EndIf
 Wend
Wend
REM show list
MsgBox s   
End Sub


Thanks JohnV that worked very well. Is it possible to identify which links are "dead" that is if the internal link being pointed to does exist or has been deleted? Basically users sometimes create internal document links then at a later date they might delete the target as they amend the document.
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 8979
Location: Lexinton, Kentucky, USA

PostPosted: Fri Feb 29, 2008 11:06 am    Post subject: Reply with quote

Quote:
Is it possible to identify which links are "dead" that is if the internal link being pointed to does exist or has been deleted?
I doubt that but if it can be done it's beyond me,
Back to top
View user's profile Send private message
jwesonga
General User
General User


Joined: 12 Feb 2008
Posts: 20

PostPosted: Mon Mar 03, 2008 12:14 am    Post subject: Reply with quote

JohnV wrote:
Quote:
Is it possible to identify which links are "dead" that is if the internal link being pointed to does exist or has been deleted?
I doubt that but if it can be done it's beyond me,


I've seen that a few people have found ways how to detect broken hyperlinks like http://www.oooforum.org/forum/viewtopic.phtml?p=276137#276137 , although no one has shared the code snippet. In essence thats what i'm trying to achieve[/url]
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