GetCrossReferenceItems is acting flakey

B

Bear

Does anyone know how to fix this behavior? I have a custom form for
cross-references. This uses the GetCrossReferenceItems method of the active
document to fill a variant array. I then unpack the array into my form list
box.

In some sessions, the lists fill only partially -- because the array is only
filled partly. For example, I may have ten chapters, but the method only
seems to return two x-refs. However, if I step through this code it works
perfectly. It almost seems like a timing issue.

I added a DoEvents before I execute method, and that seemed to help for
awhile, but now it's misbehaving again.

Any ideas?
 
D

Doug Robbins - Word MVP

You have not even shown us the haystack in which you would like us to find
the needle.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

Bear

Doug:

I hope by that you meant you wanted to see the code. It's in a custom
userform. Here it is. When I hit this sub, I've already loaded the varRefType
with the type I've selected by clicking an option button.

This works perfectlly when I step through it after clicking to select
"Chapter". I get ten items in the list. But when I run it I only get two
items in the list. I've used debug.print to determine that the ubound of
varXRefs is 10 when I step it, and 2 when I run it.

I don't think it's a code problem, because it works perfectly with Word
2000, but not Word 2003. After I put the DoEvents statement in about six
months ago, it worked for Word 2003 as well. But now it's stopped working.

I'm looking for a known issue where the method is sometimes flakey, and for
a workaround.

(Declarations)

Dim varXRefs As Variant

Sub UpdateItemList()

' Use varRefType to fill the Item list accordingly

Me.lstItem.Clear

' Prevent a partial or empty Item list in Word 2003
DoEvents

varXRefs = ActiveDocument.GetCrossReferenceItems _
(ReferenceType:=varRefType)
For Index = 1 To UBound(varXRefs)
Me.lstItem.AddItem varXRefs(Index)
Next Index

If Me.lstItem.ListCount > 0 Then
Me.lstItem.ListIndex = 0
Me.cmdInsert.Enabled = True
Else
Me.cmdInsert.Enabled = False
End If

End Sub
 
D

Doug Robbins - Word MVP

Yes, that was what I meant

I just knocked up a userform with a list box (Listbox1) and a command button
containing the following code:

ListBox1.List = ActiveDocument.GetCrossReferenceItems _
(ReferenceType:=wdRefTypeHeading)

Note: You can just set the .List attribute of a List to an array; there is
no need to iterate through the array and use the AddItem command

and then running it on a document that contained many more than 10 headings,
it populated the listbox with all of the headings.

If you want to send me the document from which you are trying to populate
the list, I will take a look and see if there is anything peculiar about it
that might be causing the problem

You can send it to dkr[atsymbol]mvps[dot]org

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

Bear

Doug:

Thanks for the tip about setting the .List attribute to the array directly!

I'm working on the idea that the behavior may be due to the code being in a
userform. I tried this:

Sub x()

Dim varXRefs As Variant

varXRefs = ActiveDocument.GetCrossReferenceItems _
(ReferenceType:="Chapter")

MsgBox "Loaded: " & Str(UBound(varXRefs))

End Sub

And almost every time I run it, I get an accurate count of the chapters in
the manual. But I swear I ran it once after doing something in the VBE (can't
remember what, but probably not directly related) and the ubound was 2 (the
wrong number). But every time after that it was 11 (the right number).

I'm still poking around and will let you know if I find anything.

Bear

--
Windows XP, Word 2000


Doug Robbins - Word MVP said:
Yes, that was what I meant

I just knocked up a userform with a list box (Listbox1) and a command button
containing the following code:

ListBox1.List = ActiveDocument.GetCrossReferenceItems _
(ReferenceType:=wdRefTypeHeading)

Note: You can just set the .List attribute of a List to an array; there is
no need to iterate through the array and use the AddItem command

and then running it on a document that contained many more than 10 headings,
it populated the listbox with all of the headings.

If you want to send me the document from which you are trying to populate
the list, I will take a look and see if there is anything peculiar about it
that might be causing the problem

You can send it to dkr[atsymbol]mvps[dot]org

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Bear said:
Doug:

I hope by that you meant you wanted to see the code. It's in a custom
userform. Here it is. When I hit this sub, I've already loaded the
varRefType
with the type I've selected by clicking an option button.

This works perfectlly when I step through it after clicking to select
"Chapter". I get ten items in the list. But when I run it I only get two
items in the list. I've used debug.print to determine that the ubound of
varXRefs is 10 when I step it, and 2 when I run it.

I don't think it's a code problem, because it works perfectly with Word
2000, but not Word 2003. After I put the DoEvents statement in about six
months ago, it worked for Word 2003 as well. But now it's stopped working.

I'm looking for a known issue where the method is sometimes flakey, and
for
a workaround.

(Declarations)

Dim varXRefs As Variant

Sub UpdateItemList()

' Use varRefType to fill the Item list accordingly

Me.lstItem.Clear

' Prevent a partial or empty Item list in Word 2003
DoEvents

varXRefs = ActiveDocument.GetCrossReferenceItems _
(ReferenceType:=varRefType)
For Index = 1 To UBound(varXRefs)
Me.lstItem.AddItem varXRefs(Index)
Next Index

If Me.lstItem.ListCount > 0 Then
Me.lstItem.ListIndex = 0
Me.cmdInsert.Enabled = True
Else
Me.cmdInsert.Enabled = False
End If

End Sub
 
B

Bear

Doug:

Here's what I've got now...

If I put that code in a userform, it only loads 2 chapters. If I then run
the plain VBA (that I sent in the previous post) the first run also returns
2, but subsequent runs return 11.

When I step through the code in the userform, it always returns 11. Here are
the macros as they are now. I'm using the Immediate window to destroy
objXRefTest after a testing session.

Sub x()

Dim varXRefs As Variant

'varXRefs = ActiveDocument.GetCrossReferenceItems _
' (ReferenceType:="Chapter")
'
'MsgBox "Loaded: " & Str(UBound(varXRefs))

Dim objXRefTest As frmXRefTest

If Not objXRefTest Is Nothing Then
Set objXRefTest = Nothing
End If

Set objXRefTest = New frmXRefTest

objXRefTest.Show

End Sub

~~~~~ userform code for Load button

Private Sub cmdLoad_Click()

Dim varXRefs As Variant

varXRefs = ActiveDocument.GetCrossReferenceItems(ReferenceType:="Chapter")

Me.txtLoaded.Text = Str(UBound(varXRefs))

Set varXRefs = Nothing

End Sub


--
Windows XP, Word 2003


Doug Robbins - Word MVP said:
Yes, that was what I meant

I just knocked up a userform with a list box (Listbox1) and a command button
containing the following code:

ListBox1.List = ActiveDocument.GetCrossReferenceItems _
(ReferenceType:=wdRefTypeHeading)

Note: You can just set the .List attribute of a List to an array; there is
no need to iterate through the array and use the AddItem command

and then running it on a document that contained many more than 10 headings,
it populated the listbox with all of the headings.

If you want to send me the document from which you are trying to populate
the list, I will take a look and see if there is anything peculiar about it
that might be causing the problem

You can send it to dkr[atsymbol]mvps[dot]org

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Bear said:
Doug:

I hope by that you meant you wanted to see the code. It's in a custom
userform. Here it is. When I hit this sub, I've already loaded the
varRefType
with the type I've selected by clicking an option button.

This works perfectlly when I step through it after clicking to select
"Chapter". I get ten items in the list. But when I run it I only get two
items in the list. I've used debug.print to determine that the ubound of
varXRefs is 10 when I step it, and 2 when I run it.

I don't think it's a code problem, because it works perfectly with Word
2000, but not Word 2003. After I put the DoEvents statement in about six
months ago, it worked for Word 2003 as well. But now it's stopped working.

I'm looking for a known issue where the method is sometimes flakey, and
for
a workaround.

(Declarations)

Dim varXRefs As Variant

Sub UpdateItemList()

' Use varRefType to fill the Item list accordingly

Me.lstItem.Clear

' Prevent a partial or empty Item list in Word 2003
DoEvents

varXRefs = ActiveDocument.GetCrossReferenceItems _
(ReferenceType:=varRefType)
For Index = 1 To UBound(varXRefs)
Me.lstItem.AddItem varXRefs(Index)
Next Index

If Me.lstItem.ListCount > 0 Then
Me.lstItem.ListIndex = 0
Me.cmdInsert.Enabled = True
Else
Me.cmdInsert.Enabled = False
End If

End Sub
 
B

Bear

Doug and all:

Here's the last thing... I wanted to make sure it wasn't because the
userform was not modal, so I created a version for the modal userform. I got
tired of commenting out sections to run the GetCrossReferenceItems in a sub
or in a form, so I put in branching code.

When I run it this way, even saying "no" and running it "raw" in the sub
fails, and loads only 2 of the 10 items!

As before, if I put in a breakpoint and step through the code, it loads 10
items either way.

Now I'm REALLY baffled.

Bear

Sub x()

Dim varXRefs As Variant
Dim strReply As String
Dim objXRefTest As frmXRefTest

strReply = MsgBox( _
Title:="DFC Tools", _
Prompt:="Run in the form?", _
Buttons:=vbYesNoCancel + vbQuestion)

Select Case strReply
Case vbYes
GoTo Form
Case vbNo
GoTo Raw
Case vbCancel
GoTo Done
End Select
GoTo Done

Raw:
varXRefs = ActiveDocument.GetCrossReferenceItems _
(ReferenceType:="Chapter")

MsgBox "Loaded: " & Str(UBound(varXRefs))
GoTo Done

Form:

Set objXRefTest = New frmXRefTest
objXRefTest.Show
GoTo Done

Done:

Set varXRefs = Nothing
Set objXRefTest = Nothing

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top