View Single Post
 
Old 04-12-2020, 06:15 AM
jpl jpl is offline Windows 7 64bit Office 2010 32bit
Advanced Beginner
 
Join Date: Jan 2016
Location: France
Posts: 33
jpl is on a distinguished road
Question Selection object and its Range property

Bonjour
Peut être pourrez vous m'aider à comprendre le problème suivant.
Il s'agit de la relation entre les objets Selection et Range.

La macro suivante compare un objet Selection et l'objet Range de cette Selection.
Elle affiche les propriétés Start et End de ces deux objets, leur propriété Text et enfin le texte du premier
élément de leur collection Characters, converti en code Asci.

Si la macro est lancée sur une sélection non vide, les résultats sont conformes à ce que l'on peut espérer.

Mais si je lance cette macro sur une sélection réduite à un point d'insertion, je ne comprends pas pourquoi :
1 la propriété Text de l'objet Range renvoie la chaine vide, alors que la propriété Text de l'objet Selection
renvoie le premier caractère qui suit le point d'insertion.
2 alors que les objets Selection et Range sont vides, leur collection Characters ne l'est pas et contient
le premier "objet" Character qui les suit ?

Pour quelle raison la propriété Text de ces deux objets est-elle différente, et pourquoi la collection
Characters n'est elle pas vide ?

Hello
Maybe you can help me understand the following problem.
This is the relationship between the Selection and Range objects.

The following macro compares a Selection object and the Range object of this Selection.
It displays the Start and End properties of these two objects, their Text property and finally the text of
the first element of their Characters collection, converted into Asci code.

If the macro is launched on a non-empty selection, the results are as expected.
But if I run this macro on a selection reduced to an insertion point, I don't understand why:
1 the Text property of the Range object returns the empty string, while the Text property of the
Selection object returns the first character after the insertion point.
2 while the Selection and Range objects are empty, their Characters collection is not and contains
the first Character "object" which follows them?

Why is the Text property of these two objects different, and why is the Characters collection not empty?
Code:
Sub ComparaisonSelectionRangeVides()
  Dim plage As Range
  Dim DébutSel As Long, FinSel As Long
  With Selection
    DébutSel = .Start: FinSel = .End
    Debug.Print "Étendue de la sélection : " & DébutSel & "->" & FinSel
    Debug.Print "Texte de la sélection : """ & .Text & """"
    Debug.Print "Premier Caractère de la sélection : " & CharacterEnAsci(.Characters(1))
  End With
  Debug.Print

'  Set plage = ActiveDocument.Range(Start:=DébutSel, End:=FinSel)
   Set plage = Selection.Range
 With plage
    Debug.Print "Étendue de la plage : " & .Start & "->" & .End
    Debug.Print "Texte de la plage : """ & .Text & """"
    Debug.Print "Premier Caractère de la plage : " & CharacterEnAsci(.Characters(1))
  End With
  Debug.Print

End Sub

Function CharacterEnAsci(Ch As Range) As String
  Dim s As String, i As Long, stemp As String
  s = Ch.Text
  For i = 1 To Len(s)
    stemp = stemp & "ch(" & Asc(Mid(s, i, 1)) & ") "
  Next i
  CharacterEnAsci = stemp
End Function
Reply With Quote