Re: Round function evaluates to zero if bookmark is hidden text

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thomas McLain wrote:
I have a document template that uses the round function, which is
used to round the value of another bookmark. When that bookmark is
hidden text, the round function evaluates to zero, which is
incorrect. When the bookmark is displayed, the round function
evaluates properly. I need the bookmark to remain hidden text, but I
need the round function to work properly. How do I do so?

It isn't the Round function that's not working properly. If you're using the
..Range.Text of the bookmark and the text is hidden, the value of that
property is returned as the empty string, "". Passing that to the Val
function, explicitly or implicitly, returns the number 0. That's what is
being passed to the Round function.

The solution is to set the TextRetrievalMode.IncludeHiddenText property of
the range to True, so it sees the text regardless of whether it's hidden.
Here's a sample:

Sub demo()
Dim myVal As Single
Dim oRg As Range

Set oRg = ActiveDocument.Bookmarks("bk1").Range
oRg.TextRetrievalMode.IncludeHiddenText = True
myVal = Round(Val(oRg.Text), 2)
MsgBox myVal
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


.



Relevant Pages

  • Re: Round function evaluates to zero if bookmark is hidden text
    ... Since you posted in a VBA newsgroup, I think my assumption was justified. ... "Jay Freedman" wrote: ... used to round the value of another bookmark. ... the round function ...
    (microsoft.public.word.vba.general)
  • Re: Round function evaluates to zero if bookmark is hidden text
    ... By embedding a REF field in your formula with the '\* Charformat' switch, you bypass the bookmarked text's hidden attribute - just make sure the 'R' in 'REF' isn't formatted as hidden. ... "Thomas McLain" wrote in message ... When that bookmark is hidden text, ... round function evaluates to zero, ...
    (microsoft.public.word.vba.general)