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/
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
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:
GoogleWWW
" by "http://www.google.com
" (as text);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.
I want to to use a macro that can perform this function, if such a macro already exists. – Anderson Green – 2011-10-03T22:20:18.680
@AndersonGreen: i've added some code for such a macro. – tohuwawohu – 2011-10-04T09:39:34.003
Would it be possible to create a function that would change any text to any sort of link? (i. e, setLink(google, http://www.google.com/))
– Anderson Green – 2011-10-04T10:50:40.440@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 – 2011-10-18T17:30:01.540