Problem with Range.Hyperlinks.count

R

robot

Hi all,

I would like to write code to unlink all hyperlinks in a given range.

I try my code on plain text and it works fine. But with a table (which
consists of a single hyperlink in each cell), I found that if Rng is a
column of cells in the table, the expression Rng.Hyperlinks.count returns
MORE than the actually number of hyperlinks. For example, if Rng comprises
the two cells cell(1,2) and cell(2,2), the hyperlink in cell(1,3) will also
be included in Rng.Hyperlinks, so the result for Rng.Hyperlinks.count is 3.

How do I tackle this problem? Any suggestions would be appreciated.

I use Word XP and Win XP.
 
H

Helmut Weber

Hi Robot,
ranges in tables are something special and
often it is faster to use the selection-object.
My theory is, that word organizes tables internally
in a linear way. So that with 5 rows and 5 columns
a range from cell(1,2) to cell(2,3) would include
7 Cells, cell 2 to cell 8.
My solution would be to address each cell individually,
like this:

Dim r As Long ' rows
Dim h As Long ' hyperlinks
Dim rng As Range
Set rng = Selection.Range
rng.Start = ActiveDocument.Tables(1).Cell(1, 2).Range.Start
rng.End = ActiveDocument.Tables(1).Cell(2, 2).Range.End
MsgBox rng.Hyperlinks.Count ' linear count
For r = 1 To 5
' individual count
h = h + ActiveDocument.Tables(1).Cell(r, 2).Range.Hyperlinks.Count
Next
MsgBox h

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
R

robot

Hi Helmut,

Thank you for your reply, which clears things up for me. I am now working on
code that will work for any selection of cells within a table.
 
J

Jezebel

Another approach, maybe a little simpler --

Dim pCell as Word.Cell
Set pCell = ActiveDocument.Tables(1).Cell(1,1)
Do
Msgbox pCell.Range.Hyperlinks.Count
Set pCell = pCell.Next
Loop until pCell is nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top