Reputation: 187
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?
Upvotes: 0
Views: 131
Reputation: 4913
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
Upvotes: 1