Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA - Hyperlink position in Word

Status
Not open for further replies.

Aj123

Programmer
Nov 8, 2001
4
GB
Hi,

I have to make a vba macro which will run through all the Hyperlinks in the current document and report it's position (x, y) in points or pixels from the top of the page.

Say there's a hyperlink which has a distance of 10 pixels from the top and 10 pixels from the left.

Is there a way to go through the hyperlinks and msgbox it's coordinates x, y relative to the top of the page.

May be we could select the hyperlink and use the Selection or Range object to find the coordinates, but I have failed to find it. Help Please!........

Many thanks,
 
Hi Aj123,

Code:
[blue]For Each k In ActiveDocument.Hyperlinks

    MsgBox "Hyperlink to " & k.Address & " is " & _
           k.Range.Information(wdVerticalPositionRelativeToPage) & _
           " points from the top, and " & _
           k.Range.Information(wdHorizontalPositionRelativeToPage) & _
           " points from the left of page " & _
           k.Range.Information(wdActiveEndPageNumber)

Next[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Thanks Tony. You da man. Makes me feels like a lumbering dinosaur. But hey, a LIVING lumbering dinosaur. I'm so slow and backwards that I make LLDs, instead of DLLs.

Gerry
 
Thanks for this brilliant code.

However, I also want to get the Height and Width of the Range or Hyperlink is that possible?

Many thanks Tony for your wonderful piece of code.
 
Hi Aj123,

This can only be approximate.

The horizontal start position of what follows the hyperlink is pretty close to the end of the hyperlink
Code:
[blue]ActiveDocument.Range(k.Range.End, k.Range.End).Information(wdHorizontalPositionRelativeToPage)[/blue]
.. should allow you to work out the width, BUT the hyperlink can spread across lines so some care is needed when using this.

Getting the height is even trickier - it has an approximate correlation to the point size of the text but, from a very quick look at some 12 point text, lines seem to alternate between 13.5 and 14.25 points so I'm not sure how to interpret the data.

The more I think about this, the more I think of possible problems! Word is designed so the User doesn't have to worry about this kind of thing, so I think you may not be able to do it perfectly. Sorry!

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Hi Tony,

Thanks again for replying, I found a way though to find the height and width of a range and it's position, I use the Information property to get the horizontal and vertical position relative to the top of the page, and then for the height and width I use the getPoint function of VBA (or is it API ??), to find the height and width.

Though there's one problem, the getPoint only works if the range or selection is visible, therefore, I have to call the ...range.select() method to select the text, this causes the text to appear in the viewable part of the screen. Then the getPoint func returns the the height and width accurately.

I know you were working on this last year, but may be some else will find it useful...

Cheers,

Aj, London, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top