Tuesday, January 23, 2018

Open office macro to create hyperlinks


I created the following macros to help me save time in creating the hyperlinks in my Links posts
There are two macros for different situations.

The following creates a hyperlink from selected text, which should be a web address. A hyperlink will be created with the hyperlink and the visible text being the selected text.

This is Open Office Basic code.
I created it by using the record macro feature.


sub Hyperlink
rem ----------------------------------------------------------------------
rem The following creates a hyperlink from selected text, which should be a web address.
rem A hyperlink will be created with the hyperlink and the visible text being the selected text.
rem define variables


rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Cut", "", 0, Array())

rem ----------------------------------------------------------------------
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Text"
args2(0).Value = "<" dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args4()) rem ---------------------------------------------------------------------- dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array()) rem ---------------------------------------------------------------------- dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array()) rem ---------------------------------------------------------------------- dim args7(0) as new com.sun.star.beans.PropertyValue args7(0).Name = "Text" args7(0).Value = ""

dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args7())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())


end sub


========================================================================

The following is used on web address that are in a separate paragraph. Position the cursor at the end of the link before activating the macro. The macro will take the text back to the beginning of the paragraph and turn it into a hyperlink, with the cursor then positioned after the hyperlink.

This is Open Office Basic code.
I created it by using the record macro feature, using the keyboard commands to select backwards to the beginning of the line, then replacing that part with the command I found to select backward to the beginning of the paragraph.



sub Hyperlink2
rem ----------------------------------------------------------------------
rem The following is used on web address that are in a separate paragraph.
rem Position the cursor at the end of the link before activating the macro.
rem The macro will take the text back to the beginning of the paragraph and turn it into a hyperlink,
rem with the cursor then positioned after the hyperlink.

rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:StartOfParaSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Cut", "", 0, Array())

rem ----------------------------------------------------------------------
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "Text"
args3(0).Value = ""

dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args5())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())

rem ----------------------------------------------------------------------
dim args7(0) as new com.sun.star.beans.PropertyValue
args7(0).Name = "Text"
args7(0).Value = "
"

dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args7())


end sub

No comments:

Post a Comment