Ask Your Question
0

How to copy selection to hyperlink in a macro?

asked 2012-08-03 11:18:53 +0200

Calimo gravatar image Calimo
1 2

updated 2013-01-29 15:10:27 +0200

qubit gravatar image qubit flag of United States
5721 3 48 41

I often need to transform a plain text DOI to a hyperlink in writer. I can do it manually but I think a macro can save me some time.

Starting with the DOI as a selection, here is the best I could do:

sub Main
  rem #----------------------------------------------------------------------
  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 #----------------------------------------------------------------------
  rem # Copy DOI
  dim doi(0) as new com.sun.star.beans.PropertyValue
  dispatcher.executeDispatch(document, ".uno:Copy", "", 0, doi())

  rem #----------------------------------------------------------------------
  rem # Create hyperlink properties
  dim hyperlink(4) as new com.sun.star.beans.PropertyValue
  hyperlink(0).Name = "Hyperlink.Text"
  hyperlink(0).Value = doi(0).Value
  hyperlink(1).Name = "Hyperlink.URL"
  hyperlink(1).Value = "http://dx.doi.org/" & doi(0).Value
  hyperlink(2).Name = "Hyperlink.Target"
  hyperlink(2).Value = ""
  hyperlink(3).Name = "Hyperlink.Name"
  hyperlink(3).Value = ""
  hyperlink(4).Name = "Hyperlink.Type"
  hyperlink(4).Value = 1

  rem #----------------------------------------------------------------------
  rem # Apply hyperlink properties
  dispatcher.executeDispatch(document, ".uno:SetHyperlink", "", 0, hyperlink())

end sub

I get an active hyperlink, but its URL is only "http://dx.doi.org/". The text contains the DOI, though.

How can I get the DOI appended to the URL?

delete close flag offensive retag edit

1 Answer

Sort by » oldest newest most voted
0

answered 2013-01-29 17:42:07 +0200

JohnSUN gravatar image JohnSUN flag of Ukraine
2358 2 23 37
http://wmstrong.ru/

Please, try to replace line

dispatcher.executeDispatch(document, ".uno:Copy", "", 0, doi())

to

doi(0).Value = ThisComponent.getCurrentSelection().getByIndex(0).getString()
link delete flag offensive edit

Login/Signup to Answer

Donate

LibreOffice is made available by volunteers around the globe, backed by a charitable Foundation. Please support our efforts: Your donation helps us to deliver a better product!

Question tools

Follow
1 follower

subscribe to rss feed

Stats

Asked: 2012-08-03 11:18:53 +0200

Seen: 173 times

Last updated: Jan 29