Identify unique references to objects from Interop Libraries (Doument.Paragraphs, etc)

前端 未结 2 716
孤独总比滥情好
孤独总比滥情好 2021-01-23 02:38

I would like to be able to identify when two interop variable objects refer to the same \"actual\" object. By \"actual\", I mean for example a given paragraph o

2条回答
  •  独厮守ぢ
    2021-01-23 03:30

    There are various ways this could be accomplished in Word. A fairly straight-forward way is to compare the Range properties using the InRange method. For example:

    Sub Tests()
    
        Dim WordApp as Word.Application = Globals.ThisAddIn.Application         
        Dim ThisDoc as Word.Document = WordApp.ActiveDocument
        Dim ThisSelection As Word.Selection = WordApp.Selection
        If ThisSelection.Range Is Nothing Then Exit Sub
    
        Dim SelectedPara As Word.Range = ThisSelection.Range.Paragraphs.First.Range
    
        For Each MyPara As Word.Paragraph In ThisDoc.Paragraphs
            Dim rng as Word.Range = myPara.Range
            If rng.InRange(SelectedPara) And SelectedPara.InRange(rng) Then
              'They're the same
            Else
              'They're not the same
            End If
            rng = Nothing
        Next
    
    End Sub
    

提交回复
热议问题