0
votes

A text

  • containins a string of hidden text

  • contains revision marks while deleted revisions are formatted as hidden.

i.e. deleted (hidden) textvisible text

The visible text has been selected (not knowing that it is preceded by hidden text) and the selection is copied to a range.

Dim R_Visible as range
set R_Visible = selection.range

If I want to determine some property, i.e. the character style, VBA will elicit the style property of the whole string "deleted (hidden) textvisible text", not only of "visible text". Thus, if the character style of the deleted part differs, from the character style of the visible part, VBA will return the value of selection.ParagraphFormat.style.

To make things more complicated I want to elicit the character style of only the 1st character of the selected text. Here are some possible approaches and their results:

a) Print R_Visible.characters(1).CharacterStyle ==> Runtime error 91

b) Print R_Visible.characters(1).Style ==> paragraph style

As a work-around I have written the following function:

Function ExactRange(Optional R_Range) As Range

Dim R_Selected As Range 

If IsMissing(R_Range) Then
    Set ExactRange = Selection.Range
Else
    'set ExactRange = R_Range.duplicate '(I haven't tested thoroughly whether that works under all circumstances)
    Set R_Selected = Selection.Range
    R_Range.Select
    Set ExactRange = Selection.Range
    R_Selected.Select 're-establishing the original selection-object 
End If

    ExactRange.SetRange Start:=ExactRange.Characters(1).End - 1, _ 
                          End:=ExactRange.Characters(Len(ExactRange)).End
End Function

which produces some more results:

c) print Mtf_ExactRange(R_Visible).style==> character style

d) print MTF_ExactRange(R_Visible).Characters(1).Style ==> paragraph style

e) print MTF_ExactRange(R_Visible.characters(1)).Style== character style

It seems to me that the behaviour is quite erratic. Can someone explain to me what is happening here and maybe point out an intended VBA-method to elicit a character style under the given circumstances without the need of a work-around function?

1
Thanks for the clarification. After I posted the assumed solution, more questions have arisen about the whole issue and I have re-formulated the post.Marcel
OK... You also need to be careful, here, about changing the content of a question once an Answer has been posted to it. Invalidating an answer worked out and posted "in good faith" is "bad" (if the positions were reversed you wouldn't be happy, I'll bet). Just mentioning it for future reference :-)Cindy Meister

1 Answers

1
votes

Selection.Style will only return one style, even if more than one has been applied. A selection can have up to 4 styles.

To find the character style of selected text, use:

Selection.Range.CharacterStyle

To get the paragraph style:

Selection.Range.ParagraphStyle

To get the table style:

Selection.Range.TableStyle

To get the list (bullets or numbering) style:

Selection.Range.ListStyle