OpenOffice.orgで選択した文字列orセルをハイパーリンク化

OpenOffice.orgで選択したURL文字列orURL文字列が入った表計算セルを、ハイパーリンク化するマクロを個人的にメモ。
元ネタはこちら「 小泉守義のPHPソースコードウォッチ

Sub AddLink

    dim documentFrame as Object
    dim documentView as Object
    dim dispatcher as Object
 
    documentView = ThisComponent.CurrentController
    documentFrame  = documentView.Frame
    dispatcher = createUnoService(“com.sun.star.frame.DispatchHelper”)
 
    dim setHyperLinkArgs(1) as new com.sun.star.beans.PropertyValue
 
    dim cell as Object
    dim cellRange as Object
    dim cellRangeAddr as Object
 
    cellRange = documentView.Selection
    cellRangeAddr = cellRange.getRangeAddress()
 
    for i = cellRangeAddr.StartColumn To cellRangeAddr.EndColumn
        for j = cellRangeAddr.StartRow To cellRangeAddr.EndRow
            cell = cellRange.getCellByPosition(i – cellRangeAddr.StartColumn, _
                    j – cellRangeAddr.StartRow)
 
            setHyperLinkArgs(0).Name = “Hyperlink.Text”
            setHyperLinkArgs(0).Value = cell.Formula
            setHyperLinkArgs(1).Name = “Hyperlink.URL”
            setHyperLinkArgs(1).Value = cell.Formula
            documentView.Select cell
            cell.Formula = “”
            dispatcher.executeDispatch(documentFrame, “.uno:SetHyperlink”, “”, 0, _
                    setHyperLinkArgs())
        next
    next
End Sub