在 OpenOffice 中创建自动链接

在 OpenOffice 中创建自动链接

OpenOffice 是否有任何宏可以使某些单词自动被超链接替换?

每次我在 OpenOffice 中输入单词“google”时,我都希望该单词变成指向http://www.google.com/

答案1

据我所知,您不能让 OOo 立即用超链接替换单词(无需编写宏),但您可以通过结合自动替换和 URL 识别来实现这一点:

  • 您可以定义一个自动替换规则,GoogleWWW用“ ” http://www.google.com(作为文本)替换“ ”;
  • 第二步,选择菜单Format-> AutoCorrect...->Apply让 OOo 用超链接替换链接文本。

我建议将“ GoogleWWW”作为要替换的文本,因为如果您只使用“google”,替换将发生第二次当创建超链接时,链接文本看起来像www.http://www.google.com.com

编辑:

下面是一个简单宏的源代码,用于用超链接替换任意选定的文本(请谨慎使用,它只是一个“概念证明” - 例如,它不检查选定的文本是否包含空格,因此生成的链接可能指向无效的 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

Tools例如,您可以使用-> Customize->KeyboardSHIFT+ CTRL+将此宏分配给键盘快捷键G。这样,您无需定义规则AutoCorrect

相关内容