Ask Your Question

Can I do a "find and replace" on the targets of hyperlinks in a draw document?

asked 2017-12-11 17:59:46 +0200

Peter Alefounder gravatar image

... and if so, how?

edit retag flag offensive close merge delete


Guess that hyperlinks do not start with 'http' or 'https' or such?

Kruno gravatar imageKruno ( 2017-12-11 19:23:36 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2017-12-21 10:19:24 +0200

librebel gravatar image

Hello @Peter Alefounder,

pending the enhancement request here, which i assumed was also yours, please try if you could use the following macro to change your Hyperlinks in Draw:

( i have not tested this in all cases, please let us know if it works in your case ):

E.g. to change only the target URLs of all Hyperlinks, you could call:

Draw_Hyperlinks_ReplaceAll( “*”, “”, 1 )

If you don’t know how to call a method with arguments, then you can put the above line inside your Main() method, and then call that Main() method via “Tools : Macros : Run Macro...”.

Sub Draw_Hyperlinks_ReplaceAll( strFind As String, strReplace As String, Optional iType As Integer )
REM Perform a Case-Sensitive Find/Replace on URL TextFields ( Hyperlinks ) inside the current Draw document.
REM <strFind>   : Case-Sensitive String to be found and replaced inside the Hyperlink's URL and/or its textual Representation.
REM <strReplace>: String to replace the found substring with, inside the Hyperlink's URL and/or its textual Representation.
REM <iType>     : [OPTIONAL] Determines which parts of the Hyperlink to Find/Replace; DEFAULT=0.
REM     0 = Find/Replace URL and Representation ; 1 = Find/Replace URL only ; 2 = Find/Replace Representation only.
REM NB. All occurrences of <strFind> within the specified parts of the Hyperlink will be replaced by <strReplace>.
REM The Search does not support Regular Expressions or wildcards.
REM But if you pass <strFind>="*" ( Unicode U+002A ASTERISK ), then all Hyperlinks will be replaced.
REM In that case <strReplace> should be a full URL.
    Dim oDoc As Object  :  oDoc = ThisComponent
    If oDoc.supportsService( "" ) Then
        Const sTextFieldURLservice  As String = ""
        Const sTextShapeType        As String = ""
        Dim oDrawPages As Object : oDrawPages = oDoc.getDrawPages()
        Dim oDrawPage As Object
        Dim oShape As Object
        Dim oText As Object
        Dim oEnum As Object
        Dim oEnumRanges As Object
        Dim oTextContent As Object
        Dim oTextRange As Object
        Dim oTextField As Object
        Dim iReplacedCount As Integer
        Dim i As Integer, j As Integer
        If IsMissing( iType ) Then iType = 0
        For i = 0 To oDrawPages.getCount() - 1      REM Traverse all DrawPages.
            oDrawPage = oDrawPages.getByIndex( i )
            For j = 0 To oDrawPage.getCount() - 1       REM Traverse all Shapes.
                oShape = oDrawPage.getByIndex( j )
                If oShape.getShapeType() = sTextShapeType Then  REM Found a TextShape.
                    oText = oShape.getText()
                    oEnum = oText.createEnumeration()
                    Do While oEnum.hasMoreElements()        REM Traverse all TextContents.
                        oTextContent = oEnum.nextElement()
                        oEnumRanges = oTextContent.createEnumeration()
                        Do While oEnumRanges.hasMoreElements()      REM Traverse all TextRanges.
                            oTextRange = oEnumRanges.nextElement()
                            If Not IsNull( oTextRange.TextField ) Then  REM Found a TextField.
                                oTextField = oTextRange.TextField
                                If oTextField.supportsService( sTextFieldURLservice ) Then      REM Found URL TextField.
                                    If strFind = "*" Or Instr( 1, oTextField.URL, strFind, 0 ) > 0 Or Instr( 1, oTextField.Representation, strFind, 0 ) > 0 Then
                                        Dim oTextFieldURL As Object
                                        oTextFieldURL = oDoc.createInstance( sTextFieldURLservice )     REM Replace URL TextField.
                                        oTextFieldURL.URL = oTextField.URL
                                        oTextFieldURL.Representation = oTextField.Representation
                                        If strFind = "*" Then
                                            If iType = 0 Or iType = 1 Then oTextFieldURL.URL = ConvertToURL( strReplace )
                                            If iType = 0 Or iType = 2 Then oTextFieldURL.Representation = strReplace
                                            If ...
edit flag offensive delete link more


Thanks for the macro. I'll see what I can do with it - not much experience with macros, so no promises!

Peter Alefounder gravatar imagePeter Alefounder ( 2018-01-04 18:45:18 +0200 )edit

answered 2018-01-04 18:39:17 +0200

Peter Alefounder gravatar image

Thanks for the comments. Over the holiday period, I found my own way to do this, to wit: save the drawing as Flat XML (.fogd) and do a global edit with a simple text editor. If there are a lot of files, use a programmable text editor and write a shell script.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-12-11 17:59:46 +0200

Seen: 226 times

Last updated: Jan 04 '18