开发者

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)开发者_如何学Python: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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜