Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble returning object from user defined collection

Status
Not open for further replies.

MajP

Technical User
Aug 27, 2005
9,382
US
I am trying to read through a Word document and create a user defined collection of the sentences that have revisions.
Code:
Public Function getChangedSentences(theDoc As Document) As Collection
  Dim snt As Range
  Dim colTemp As New Collection
  For Each snt In theDoc.Sentences
     If snt.Revisions.Count > 0 Then
       colTemp.Add snt
     End If
  Next snt
  Set getChangedSentences = colTemp
End Function

This kind of appears to work except it appears that the collection is populated with the sentence text and not a sentence range object.

I would like to then read through the returned collection and return the sentence objects, but I can only return the text. Here is the demo
Code:
Public Sub testCol()
  Dim chngs As Collection
  Dim snt As Object
  Dim i As Integer
  Set chngs = getChangedSentences(ActiveDocument)
  For i = 1 To chngs.Count
    Debug.Print chngs.Item(i)
    Debug.Print VarType(chngs.Item(i))
    'set snt = chngs.item(i)
  Next i
End Sub

It prints the text and returns a varType of 8 for a string. I would not expect that to work and I would expect an object vartype. If I try to set an item from the collection = an object variable, I get an "object required error".
It appears that
colTemp.Add snt
Is defaulting to
colTemp.add snt.text

Obviously I am missing something fundamental. Any help would be appreciated.
 
It appears that
colTemp.Add snt
Is defaulting to
colTemp.add snt.text

That is correct.

"This kind of appears to work except it appears that the collection is populated with the sentence text and not a sentence range object.

That is correct.

I would like to then read through the returned collection and return the sentence objects, but I can only return the text. "

And do what???? If you want to do something with the text, then there it is ...you can. What do you need to do?

It IS possible to make a collection of range object, and Set a range object to a collection item.


Code:
Public Function getChangedSentences(theDoc As Document) As Collection
  Dim snt As Object
  Dim r As Range
  Dim colTemp As New Collection
  For Each snt In theDoc.Sentences
     If snt.Revisions.Count > 0 Then
[COLOR=red]      Set r = snt
       colTemp.Add r[/color red]
     End If
  Next snt
  Set getChangedSentences = colTemp
End Function

Public Sub testCol()
  Dim chngs As Collection
  Dim r As Range
    Set chngs = getChangedSentences(ActiveDocument)
    Set r = chngs.Item(1)
[COLOR=red]' and do....what???????[/color red]
End Sub

Gerry
 
The reason I ask, is that in your For loop:
Code:
  For i = 1 To chngs.Count
    Debug.Print chngs.Item(i)
    Debug.Print VarType(chngs.Item(i))
    'set snt = chngs.item(i)
  Next i
End Sub
what exactly are you doing with each range object in the collection (assuming you can get a range object)?

Even if you DO get a range object from the collection:
Code:
    Debug.Print chngs.Item(i)
    Debug.Print VarType(chngs.Item(i))
will still give the same result, as the default value of chngs.Item(x) IS the text.

That being said, again, you CAN get a range object out of this, if you want. To demonstrate this, I added a message string getting each Range.Start value of the Revised sentence. You can only get this from a true range object. Putting the cursor at the end of the document - so the message is "typed" at the end:
Code:
Public Sub testCol()
  Dim chngs As Collection
  Dim r As Range
  Dim j As Long
  Dim msg As String
    Set chngs = getChangedSentences(ActiveDocument)
      For j = 1 To chngs.Count
         Set r = chngs.Item(j)
         msg = msg & "Changed sentence " & j & _
            " range start = " & r.Start & vbCrLf
         Set r = Nothing
      Next j
   Selection.TypeText msg
End Sub

Sample result?

Changed sentence 1 range start = 91
Changed sentence 2 range start = 648
Changed sentence 3 range start = 973

The collection is a collection of range objects, and a range object SET to an item has all the properties and methods of a range object. For example:
Code:
    Set chngs = getChangedSentences(ActiveDocument)
      For j = 1 To chngs.Count
         Set r = chngs.Item(j)
         MsgBox r.Information(wdActiveEndPageNumber)
would display the page number of that current range object from the collection.

Gerry
 
Thanks, but I ran this in 2003 and it works fine. I will have to try it tomorrow in 2007 again. I push a range object in and now pull out a range object out. I plan to eventually do multiple things with the range object, but I could use none of the properties because I could not return a range. There must have been somethingelse going on. I appreciate your time and response.

To demonstrate this is a slight modification, but the same idea. And for some reason now it works.
Code:
Public Function getChangedSentences(theDoc As Document) As Collection
  Dim snt As Range
  Dim colTemp As New Collection
  For Each snt In theDoc.Sentences
     If snt.Revisions.Count > 0 Then
       colTemp.Add snt
     End If
  Next snt
  Set getChangedSentences = colTemp
End Function

Public Sub testCol()
  Dim chngs As Collection
  Dim snt As Range
  Set chngs = getChangedSentences(ActiveDocument)
  For Each snt In chngs
    Debug.Print snt.Text
    Debug.Print snt.Revisions.Count
    Debug.Print snt.Start
  Next snt
End Sub

In the function snt is declared as a range and
"For Each snt In theDoc.Sentences"
will return snt as a range.
I understand that .text is the default property of a range, but there is no reason that it should populate the new collection with a string not the range object.
Therefore the addition of
Set r = snt
is redundant.
 


You could store the indices to the sentence collection, that you want to use, in an array.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks. I may do that to get better performance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top