Delete all hyperlinks - Writer

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Delete all hyperlinks - Writer

Post by JohnV »

My test document contained the following elements and each element contained at least one hyperlink:
Normal text,
A user field,
A database field,
A table,
A table within that table,
A frame and
A table within that frame.

Code: Select all

Option Explicit
Sub DeleteAllHyperlinks 'In a Writer document
Dim oDoc,enum1,TTs,thisT,i,col,row,oCell,oCur,txt,TFs,thisTF
oDoc = ThisComponent
REM Process normal text.
enum1 = oDoc.Text.createEnumeration
Enumerate(enum1)
REM Process tables.
TTs = oDoc.getTextTables
For i = 0 to TTs.Count-1
 thisT = TTs.getByIndex(i)
 For col = 0 to thisT.Columns.Count-1
  For row = 0 to thisT.Rows.Count-1
   oCell = thisT.getCellByPosition(col,row)
   oCur = oCell.createTextCursor
   txt = oCur.getText
   enum1 = txt.createEnumeration
   Enumerate(enum1)
  Next
 Next  
Next
REM Process frames.
TFs = oDoc.getTextFrames
For i = 0 to TFs.getCount-1
 thisTF = TFs.getByIndex(0)
 enum1 = thisTF.createEnumeration
 Enumerate(enum1)
Next
End Sub

Sub Enumerate(enum1)
Dim thisPara,enum2,thisPortion
While enum1.hasMoreElements
 thisPara = enum1.nextElement
 TableCheck:
 If thisPara.SupportsService("com.sun.star.text.TextTable") then 
  If enum1.hasMoreElements then
    thisPara = enum1.nextElement
    Goto TableCheck
   Else Exit Sub
  EndIf
 EndIf
 enum2 = thisPara.createEnumeration
 While enum2.hasMoreElements
  thisPortion = enum2.nextElement
  thisPortion.HyperlinkTarget = ""
  thisPortion.HyperLinkURL = ""
 Wend
Wend
End Sub
Post Reply