3

Are there any macros for OpenOffice that enable certain words to automatically be replaced with hyperlinks?

Every time I type the word "google" in OpenOffice, I want the word to become a hyperlink to http://www.google.com/

1 Answer 1

1

AFAIK you can't let OOo immediately replace a word by a hyperlink (without writing a macro), but you can achieve this by combining AutoReplace and URL recognition:

  • You could define a AutoReplace rule replacing "GoogleWWW" by "http://www.google.com" (as text);
  • in a second step, select Menu Format -> AutoCorrect... -> Apply to make OOo replace the link text by a hyperlink.

I've proposed "GoogleWWW" as Text to replace, because if you use simply "google", the replacement will take place a second time when the hyperlink is created, resulting in a link text looking like www.http://www.google.com.com.

EDIT:

Here's the source of a simple macro to replace arbitrary selected text by a hyperlink (use with care, it's just a "proof of concept" - for example, it doesn't check if the selected text contains spaces, so the resulting link may point to a invalid url):

sub ReplaceByHyperlink
    rem ----------------------------------------------------------------------
    rem define variables
    dim document   as object
    dim dispatcher as object
    dim oSelection, oRange as object
    dim strSelectedWord as String
    rem ----------------------------------------------------------------------
    rem get access to the document and grab first selection
    oSelection = ThisComponent.CurrentController.Selection
    oRange = oSelection(0)
    rem ----------------------------------------------------------------------
    rem rudimentary input check (selection available, text selected?)
    If Not (HasUnoInterfaces(oRange, "com.sun.star.text.XTextRange")) Then
        MsgBox "no text available"
        exit sub
    End if
    strSelectedWord = oRange.getString
    If Len(strSelectedWord) < 1 Then
        MsgBox "No Text selected"
        exit sub
    End if
    rem ----------------------------------------------------------------------
    rem ok, there's some text selected, let's transform it...
    document   = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    dim args1(4) as new com.sun.star.beans.PropertyValue
    args1(0).Name = "Hyperlink.Text"
    args1(0).Value = strSelectedWord
    args1(1).Name = "Hyperlink.URL"
    args1(1).Value = "http://www." + LCase(strSelectedWord) + ".com/"
    args1(2).Name = "Hyperlink.Target"
    args1(2).Value = ""
    args1(3).Name = "Hyperlink.Name"
    args1(3).Value = strSelectedWord
    args1(4).Name = "Hyperlink.Type"
    args1(4).Value = 1
    dispatcher.executeDispatch(document, ".uno:SetHyperlink", "", 0, args1())
end sub

You can assign this macro to a keyboard shortcut using Tools -> Customize -> Keyboard, to SHIFT+CTRL+G, for example. This, way, you don't need to define a AutoCorrect rule.

4
  • I want to to use a macro that can perform this function, if such a macro already exists. Oct 3, 2011 at 22:20
  • @AndersonGreen: i've added some code for such a macro.
    – tohuwawohu
    Oct 4, 2011 at 9:39
  • Would it be possible to create a function that would change any text to any sort of link? (i. e, setLink(google, google.com)) Oct 4, 2011 at 10:50
  • @AndersonGreen: surely that's possible. I've modified my answer so the macro changes selected text into a www.[selection].com hyperlink. But it will require some more work, since it accepts any text (e.g. containing spaces and so on) as part of the hyperlink target. You may want to check if the selected text is a valid host name.
    – tohuwawohu
    Oct 18, 2011 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .