From: Bear on
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" wrote:

> 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" <david.chinell(a)ge.com(nospam)> wrote in message
> news:313A40E0-DF7F-4C33-A051-F721BF2414E5(a)microsoft.com...
> > 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
> >
> >
> > --
> > Windows XP, Word 2003
> >
> >
From: Bear on
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