views:

699

answers:

1

How do you add a Hyperlink to a word document using an existing bookmark. I have been testing using IRB but continue to get Command Failed. I have attached to a running word application have text selected that I want to tie to the hyperlink. For testing I have been trying to just add a google hyperlnk. I figure once I get that then I would be able to figure out the bookmark. This is the command I am using

doc.Hyperlink.add(word.selection, 'http://www.google.com', '','','text to display')

The two blank parms are for SubAddress and ScreenTip respectivly.

+1  A: 

Luke-

You're very close...

Change this...

  doc.Hyperlink.add(word.selection, 'http://www.google.com', '','','text to display')

...to this...

  doc.Hyperlinks.add(word.selection.Range, 'http://www.google.com', '','','text to display')

There were two changes necessary:

(1) You call the Add method on the Hyperlinks (plural) collection, and (2) the first argument needs to be a Range object.

With these changes, your code works for me.

David Mullet
Verified as correct. See this page about using Ruby and word together: http://rubyonwindows.blogspot.com/2007/04/automating-word-with-ruby-application.html Basically: **1)** Start word, type some text and select it **2)** In IRB `require 'win32ole'` **3)** `word = WIN32OLE.connect('Word.Application')` to connect to word **4)** `doc = word.ActiveDocument` to get current doc **5)** Paste in code from @David Mullet to test.
i5m