adam reed
adam reed

Reputation: 2024

Win32ole, method to read URL from hyperlink instead of text? (Ruby, but not required)

I'm working with WIN32OLE to read some data files in various states of disarray. I've been able to read text from the majority of the files now using the Sentences and Shapes methods, however, for files with hyperlinks I am only able to read the hyperlink text, not the underlying URL.

I've been digging through the methods/ole_methods of 'hyperlinks' and have tried many, but none have released any properties of the hyperlink other than the text (appropriately so, in some cases).

irb(main):084:0> doc.Sentences(4).hyperlinks.inspect
=> "#<WIN32OLE:0x2c233d0>"

irb(main):085:0> doc.Sentences(4).hyperlinks.text
=> "Hi I'm a link!"

The most relevant google results deal with adding links to Excel, using the 'address' tag which is not a method of hyperlinks. Others reference 'action', which also does not seem to be a method.

Upvotes: 0

Views: 405

Answers (2)

Paul Hoffer
Paul Hoffer

Reputation: 12906

This works for me:

doc.Hyperlinks.each do |x|
  puts x.Address
end

EDIT: I got this to work for grabbing the links out of TextBoxes too.

doc.Shapes.each do |x|
  x.TextFrame.TextRange.Hyperlinks.each do |i|
    puts i.Address
  end
end

Both of these give the full address, regardless of the display text.

Upvotes: 2

DarinH
DarinH

Reputation: 4879

The HyperLinks object is a collection of HyperLink objects.

Each one contains an "Address" property that should point to the URL of the link, and a "textToDisplay" property.

I don't know anything about ruby or the win32ole lib, but can you get to the Hyperlink objects in the collection, maybe like this:

irb(main):084:0> doc.Sentences(4).hyperlinks(1).TextToDisplay

irb(main):084:0> doc.Sentences(4).hyperlinks(1).Address

Upvotes: 1

Related Questions