Help with Range.TextRetrievalMode?

J

Julian

Just come across this oddity... (Word 2002, under Vista HP)

I don't seem to be able to change the values for TextRetrievalMode
(includehiddentext or includefields) when the range it is applied to is a
comment's range (mycomment.range)... Trying different view types on the
TextRetrievalMode object doesn't help either.

And for different comments the TextRetrievalMode settings seem different
(and inconsistent - so I can't compare comments for their "normal" text)

any ideas?
 
T

Tony Jollans

It works for me (2002, Vista Ultimate)

Set CommentRange = ActiveDocument.Comments(1).Range.Duplicate
Debug.Print CommentRange.Text
CommentRange.TextRetrievalMode.IncludeHiddenText = True
Debug.Print CommentRange.Text

produces

This comment hides text
This comment hides some hidden text

(a correct representation of my comment)

Can you post your code?
 
J

Julian

I'll get back with some code later - after I have tested something based on
your example: I noticed immediately that you worked with a range duplicate
whereas I was working directly on the comment range. I don't immediately see
why that should make any difference but it's worth checking!

Thanks
 
J

Julian

OK - update.

Two discoveries:

1. You cannot set TextRetrievalMode with comment.range.TextRetrievalMode...
you have to assign the comment range to a range object and work with that

2. You cannot create e.g. an Index Field directly in a comment - but if you
copy and paste one into a comment, TextRetrievalMode.HiddenText = false gives
the plain text, and TextRetrievalMode.HiddenText = true includes the field
code text regardless of the IncludeFieldCode setting (i.e. Field Code is not
recognised as such and is treated purely as hidden text, which is how it is
formatted)

3. hidden text visibility (UI setting) determines the default state of
..IncludeHiddenText - i.e. if hidden text is visible and you *don't* want it,
you must explicitly turn it off...

That probably accounts for the problems I was having... some text was pasted
into a comment and brought a (hidden) Index Entry field code with it, and I
was working directly on the comment range and not indirectly via a range
object (doesn't need to be a duplicate)

Thanks for prodding me along...

Julian

PS Code that illustrates the above (for suitable comments!) as follows...

Sub testCommentHiddentText()
Dim aCom As Comment
Dim aStr As String
Dim aRange As Range

Set aCom = Selection.Comments(1)
aStr = aCom.Range.Text

Debug.Print "Working directly on comment range"
Debug.Print "Incl. Hidden Text = " & vbTab &
aCom.Range.TextRetrievalMode.IncludeHiddenText & vbTab & " Incl Field Codes =
" & vbTab & aCom.Range.TextRetrievalMode.IncludeFieldCodes & vbTab & " Result
= " & vbTab & aCom.Range.Text
aCom.Range.TextRetrievalMode.IncludeHiddenText = True
Debug.Print "Incl. Hidden Text = " & vbTab &
aCom.Range.TextRetrievalMode.IncludeHiddenText & vbTab & " Incl Field Codes =
" & vbTab & aCom.Range.TextRetrievalMode.IncludeFieldCodes & vbTab & " Result
= " & vbTab & aCom.Range.Text
aCom.Range.TextRetrievalMode.IncludeFieldCodes = True
Debug.Print "Incl. Hidden Text = " & vbTab &
aCom.Range.TextRetrievalMode.IncludeHiddenText & vbTab & " Incl Field Codes =
" & vbTab & aCom.Range.TextRetrievalMode.IncludeFieldCodes & vbTab & " Result
= " & vbTab & aCom.Range.Text

Debug.Print "Working on range object from Comment range"
Set aRange = aCom.Range
Debug.Print "Incl. Hidden Text = " & vbTab &
aRange.TextRetrievalMode.IncludeHiddenText & vbTab & " Incl Field Codes = " &
vbTab & aRange.TextRetrievalMode.IncludeFieldCodes & vbTab & " Result = " &
vbTab & aRange.Text
aRange.TextRetrievalMode.IncludeHiddenText = True
Debug.Print "Incl. Hidden Text = " & vbTab &
aRange.TextRetrievalMode.IncludeHiddenText & vbTab & " Incl Field Codes = " &
vbTab & aRange.TextRetrievalMode.IncludeFieldCodes & vbTab & " Result = " &
vbTab & aRange.Text
aRange.TextRetrievalMode.IncludeFieldCodes = True
Debug.Print "Incl. Hidden Text = " & vbTab &
aRange.TextRetrievalMode.IncludeHiddenText & vbTab & " Incl Field Codes = " &
vbTab & aRange.TextRetrievalMode.IncludeFieldCodes & vbTab & " Result = " &
vbTab & aRange.Text

Debug.Print "Working on duplicate range object from Comment range"
Set aRange = aCom.Range.Duplicate
Debug.Print "Incl. Hidden Text = " & vbTab &
aRange.TextRetrievalMode.IncludeHiddenText & vbTab & " Incl Field Codes = " &
vbTab & aRange.TextRetrievalMode.IncludeFieldCodes & vbTab & " Result = " &
vbTab & aRange.Text
aRange.TextRetrievalMode.IncludeHiddenText = True
Debug.Print "Incl. Hidden Text = " & vbTab &
aRange.TextRetrievalMode.IncludeHiddenText & vbTab & " Incl Field Codes = " &
vbTab & aRange.TextRetrievalMode.IncludeFieldCodes & vbTab & " Result = " &
vbTab & aRange.Text
aRange.TextRetrievalMode.IncludeFieldCodes = True
Debug.Print "Incl. Hidden Text = " & vbTab &
aRange.TextRetrievalMode.IncludeHiddenText & vbTab & " Incl Field Codes = " &
vbTab & aRange.TextRetrievalMode.IncludeFieldCodes & vbTab & " Result = " &
vbTab & aRange.Text

End Sub
 
T

Tony Jollans

Glad you've worked it all out. Well done!

Just one comment (no pun intended) on your point 1. It is generally better
(in all sorts of cases) to work with your own ranges rather than those
returned by range properties of various document objects. In a nutshell:
your own objects tend to do what you tell them - document objects do what
the document tells them.
 
J

Julian

Thanks Tony -
Just one comment (no pun intended) on your point 1.

of the pair in 3 parts!
It is generally better
(in all sorts of cases) to work with your own ranges rather than those
returned by range properties of various document objects. In a nutshell:
your own objects tend to do what you tell them - document objects do what
the document tells them.

Yeah, I do sort of know that, but it's a workaround for an I/F inconsistency
rather than a solid principle IMO: unless one uses range.duplicate an
assigned range object *should* just be a separate reference to the same
underlying object - so either the method should work along both routes or it
should work along neither...

It's a bit annoying having to program defensively to avoid such
obscurities... but I can think of worse problems <g>

....really ought to compile all my assorted "despite what I thought/the
documentation says..." code comments into a "Warning - this doesn't do what
you expect!" list.

Hmmm... I could write some code to strip them out automatically... LOL

Ta,

Julian
 

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