Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ActiveDocument.Characters.Count and hyperlinks

219 views
Skip to first unread message

da9ve

unread,
May 5, 2010, 12:16:01 PM5/5/10
to
I've noticed that when running a macro in Word, the value
ActiveDocument.Characters.Count does not include characters 'hidden' in
hyperlinks.

For example, if I set the selection range to

ActiveDocument.Range([count] - 1, [count])

... and look at the selection on-screen, it will not be the last character
in the document.

Is this a known issue? Is there an easy command to simply remove all
hyperlinks in a document, accomplishing the same thing as right-clicking and
selecting "Remove Hyperlink"?

Doug Robbins - Word MVP

unread,
May 5, 2010, 6:18:36 PM5/5/10
to
Try using:

Dim charcount As Long
Dim i As Long
charcount = 0
With ActiveDocument
For i = 1 To .Fields.Count
charcount = charcount + .Fields(i).Code.Characters.Count
Next i
charcount = charcount + ActiveDocument.Characters.Count
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"da9ve" <da...@discussions.microsoft.com> wrote in message
news:163788B3-9825-4521...@microsoft.com...

da9ve

unread,
May 6, 2010, 11:53:01 AM5/6/10
to

Brilliant, in and of itself. This helps some, especially as it would seem to
account for all fields and not just hyperlinks. But I'm still seeing a
discrepancy in the counts - even with the field characters, it still falls
short, and by different amounts when the fields are toggled "on" vs. "off" -
I was kinda hoping there'd be a state where everything would be consistent.

I was gong to muse that it might be some extra hidden html that doesn't get
caught as a field, since some of the text in my test document was pasted in
from a web page,.... but on further tweaking, when I simply Remove both the
hyperlinks, the counts come out perfectly consistent, so I'm convinced that
it's something peculiar to the hyperlink fields themselves. What's the deal
with those funky outlined { } braces that delimit the field?

da9ve

Tony Jollans

unread,
May 6, 2010, 12:32:09 PM5/6/10
to
There really isn't anything simple you can do about this. It just doesn't
work the way you want. The Range you are working with (and therefore the
number of characters in it) is essentially what would show on screen - for
hyperlinks, or, more generally, for fields, you either see the field code or
the field result (and never the internal special separator characters). You
can control this to an extent - the equivalent of showing field codes - with
the .TextRetrievalMode options, but it won't do everything you want.

What exactly do you want to achieve?

--
Enjoy,
Tony

www.WordArticles.com

"da9ve" <da...@discussions.microsoft.com> wrote in message

news:F1D18A8C-94B6-433C...@microsoft.com...
>A specific example. A hyperlink with the field codes toggled looks like:
>
> {HYPERLINK
> "http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceSymbols.htm"
> \t "winout"}
>
> (... of course with the funky special { } braces).
>
> The characters in the link text itself, in this case:
>
> http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceSymbols.htm
>
> ... are simply omitted from the count in ActiveDocument.Characters.Count.

da9ve

unread,
May 6, 2010, 2:18:10 PM5/6/10
to
The outside-the-box way to handle this would be to figure out how to
gracefully catch and handle whatever error condition comes back when I try to
select a character that's outside the real range of the ActiveDocument, I
suppose. I'm emphatically NOT any kind of VBA guru, and just don't have the
vocabulary yet to know how to do this, though.

Thanks for any clues.

da9ve

"Tony Jollans" wrote:

> .
>

Doug Robbins - Word MVP

unread,
May 6, 2010, 5:25:10 PM5/6/10
to
The field delimiters are probably not counted, as the .Code.Text of the
field just returns the text inside them. You probably need to use:

Dim charcount As Long
Dim i As Long
charcount = 0
With ActiveDocument
For i = 1 To .Fields.Count

charcount = charcount + .Fields(i).Code.Characters.Count + 2


Next i
charcount = charcount + ActiveDocument.Characters.Count
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"da9ve" <da...@discussions.microsoft.com> wrote in message

news:BC85372E-BC5D-4558...@microsoft.com...

Tony Jollans

unread,
May 7, 2010, 4:06:11 AM5/7/10
to
> ... and just don't have the

> vocabulary yet to know how to do this, though.

What, exactly, is 'this'. There may be a better way to do whatever it is
that you are trying to do,

--
Enjoy,
Tony

www.WordArticles.com

"da9ve" <da...@discussions.microsoft.com> wrote in message

news:7A9BB884-1693-4A3C...@microsoft.com...

da9ve

unread,
May 7, 2010, 9:37:01 AM5/7/10
to

"Tony Jollans" wrote:

> > ... and just don't have the
> > vocabulary yet to know how to do this, though.
>
> What, exactly, is 'this'. There may be a better way to do whatever it is
> that you are trying to do,

The specific "this" I was referring to is 'capturing and handling an error
condition that I would expect to be returned if I try to select a character
using a numerical range that extends past the end of the document.' Being
able to do that would get me away from even bothering with calculating the
total number of characters in the document, which, as we've figured out
elsewhere in this thread, has to include counts for characters in fields plus
field delimiters; and it would still ensure that every character in the
document would get checked.

The bigger "this" that's the purpose of this macro - the 'check' that it's
doing on every character - involves ferreting out every use of Symbol font -
whether as plain formatted Symbol font (easy) or alternately created by the
Insert Symbol tool with Symbol font selected. As I learned in some other
threads specific to that font, it seems to be treated in very unusual and
special ways, and the logic to find it had to include another method besides
just a simple global Find/Replace on Symbol as the font name. (There's also
some logic that goes after a couple specific troublesome Unicode ranges, but
that part was pretty straight-forward.)

I'm satisfied with the logic that others supplied in those other threads
around the Symbol font thing, and that part of the macro works fine. So I'm
really only pursuing obviating the character count complexity.

thanks!
da9ve

da9ve

unread,
May 7, 2010, 9:57:01 AM5/7/10
to

"Doug Robbins - Word MVP" wrote:

> The field delimiters are probably not counted, as the .Code.Text of the
> field just returns the text inside them. You probably need to use:
>
> Dim charcount As Long
> Dim i As Long
> charcount = 0
> With ActiveDocument
> For i = 1 To .Fields.Count
> charcount = charcount + .Fields(i).Code.Characters.Count + 2
> Next i
> charcount = charcount + ActiveDocument.Characters.Count
> End With


Yep, I eventually arrived at that myself, based on the margin still left
over at the end when the macro finishes running. That works pretty well for
the case where all the fields are toggled to not show the field codes, but
when any of them are toggled to SHOW field codes, the counts change in ways I
haven't fully figured out yet. But expecting fields to be toggled to hide
field codes is a very reasonable assumption, so I'm going with it for now.

gracias,
da9ve

Tony Jollans

unread,
May 7, 2010, 11:34:11 AM5/7/10
to
Well, the easiest thing would be to avoid the error by checking against
ActiveDocument.Content.End

--
Enjoy,
Tony

www.WordArticles.com

"da9ve" <da...@discussions.microsoft.com> wrote in message

news:732928D1-E29B-4740...@microsoft.com...

Fumei2 via OfficeKB.com

unread,
May 10, 2010, 2:56:30 PM5/10/10
to
I am still trying to get my head around:

if I try to select a character using a numerical range that extends past the
end of the document.'

How can anything be past the end of the document??

da9ve wrote:
>> The field delimiters are probably not counted, as the .Code.Text of the
>> field just returns the text inside them. You probably need to use:

>[quoted text clipped - 8 lines]


>> charcount = charcount + ActiveDocument.Characters.Count
>> End With
>
>Yep, I eventually arrived at that myself, based on the margin still left
>over at the end when the macro finishes running. That works pretty well for
>the case where all the fields are toggled to not show the field codes, but
>when any of them are toggled to SHOW field codes, the counts change in ways I
>haven't fully figured out yet. But expecting fields to be toggled to hide
>field codes is a very reasonable assumption, so I'm going with it for now.
>
>gracias,
>da9ve

--
Gerry

Message posted via http://www.officekb.com

da9ve

unread,
May 20, 2010, 1:29:01 PM5/20/10
to

"Fumei2 via OfficeKB.com" wrote:

> I am still trying to get my head around:
>
> if I try to select a character using a numerical range that extends past the
> end of the document.'
>
> How can anything be past the end of the document??
>

It can't, and that's my point. The existing logic in the macro leads me to
step through the document, character by character, using:

ActiveDocument.Range(i,i+1).Select

...and incrementing i. When i+1 happens to exceed the location of the last
character in the document, I want to be able to just handle the error
condition that arises, but I flat-out don't know enough VBA to do that. It
seems like this should be a really easy question. What I expect to be able
to do would be something like:

On Error (*some unknown-to-me error code resulting from the above expression
crashing up against the end of the document*) Exit Do

da9ve

da9ve

unread,
May 21, 2010, 11:02:01 AM5/21/10
to
This turned out to be the solution.

ActiveDocument.Content.End gives the same number as the more complicated
method of adding ActiveDocument.Characters.Count PLUS the number of
characters in all fields, PLUS 3 "overhead" characters for each field (which
I determined experimentally), AND it gives a valid, usable number whether the
state of the fields is toggled on or off - which the adding method does not
(also determined experimentally).

So, muchas gracias.

da9ve


"Tony Jollans" wrote:

> .
>

0 new messages