Question

TiLSol on Tue, 22 Jul 2014 09:56:40


Dear all,

I'm writing a function that should populate a listbox with all references to a custom referencetype. However, the function WdReferenceType seems to be quite unreliable. Sometimes it provides me with the correct number of references but sometimes it limits the amount of references to e.g. 6 while there should be more than 100 references. Whenever I run the code in the debug mode it works fine.

I've been searching around the internet and the problem has been brought up a number of times before. But no decent explanations and/or workarounds have been provided. 

So I'm wondering of any decent workaround exists for this behavior. I've added my code below just for information. I'm using visual studio 2013 to write an add-in for word 2013.

Hoping for an answer,

  Tim

   Sub changeitall()

        Dim objApp As Word.Application = Globals.ThisAddIn.Application
        Dim objDoc As Word.Document = objApp.ActiveDocument
        Dim a As Array
        Dim i As Long
        Dim customhead as string

        Me.lbrefs.Items.Clear()
        
        System.Windows.Forms.Application.DoEvents()

customhead = cboreftype.SelectedItem
        objDoc.GetCrossReferenceItems(customhead)

        System.Windows.Forms.Application.DoEvents()
        For i = 1 To a.Length
            Me.lbrefs.Items.Add(a(i))
        Next

        a = Nothing
        objApp = Nothing
        objDoc = Nothing

    End Sub


Sponsored



Replies

Cindy Meister MVP on Tue, 22 Jul 2014 15:21:52


The only recommendation I've ever seen that has seemed at all reliable has been to work *by range*, rather than with the entire document. Loop all the paragraphs, for example.

Alternatively, since this is the new file format (Word 2007 or later) you do have the option of parsing the WordOpenXML: objDoc.Content.WordOpenXML

will return a string with the flat-file Word Open XML for the document. All the reference information will be in that and can be analyzed. Writing anything back is another question, entirely, but if you only need to read and create a list...

TiLSol on Wed, 23 Jul 2014 08:43:41


Hi Cindy,

Thanks for the answer.

By looping through the paragraphs I'm assuming you mean checking the style of each paragraph. This is a bit less straight forward as the solution should apply to multiple templates (and thus multiple styles) and not all employees will probably correctly put all the captions in the correct style.

I'm interested in the WordOpenXML solution though. Could you provide me with some references as how to get started on this. The whole open XML thing is pretty new to me.

Cindy Meister MVP on Wed, 23 Jul 2014 15:53:09


Hi TilSol

<<By looping through the paragraphs I'm assuming you mean checking the style of each paragraph.>>

No, not the styles - your question is about the References collection, if I read your post correctly.

I meant to loop the paragraphs (or sections - something small enough to reliably return the members of the collection) and check their Ranges for the References. But now I realize I was thinking "Revisions" and you mean Cross References - sorry about misunderstanding your question first time around!

In that case, it should be possible to work with the FIELDS collection of the document. If you press Alt+F9 it will toggle field results to field code display. This is what's actually behind the cross references and you can access them directly - you just need to analyze what characteristics of the field codes you need to pick up in order to populate your list. It's a bit more round-about than accessing what GetCrossReferences returns, but it should be more reliable.

Open XML: The best starting place is OpenXMLDeveloper.org, but it would be a huge learning curve if you've little to no prior experience with XML.

TiLSol on Fri, 25 Jul 2014 08:13:39


Hi Cindy,

Thanks again for the answer.

The FIELDS collection looks indeed very promising. But there is however one minor glitch which causes me some trouble.

In most of the templates (but not all) we use, the captions do include the chapter numer with a hyphen as seperator. So you get something like: "Figure 1-3: xxx".

The hyphen is however always ommitted upon requesting the result from the FIELDS collection (field.result.text). So the result looks more like "Figure 13: xxx". I might figure out a workaround by checking in which chapter the figure / table is located and parse the caption based on this information. However, as not all the templates have use the hyphen things might become messy.

Therefore my (last) question is whether there is a cleaner method to make the hyphen appear based on the information in the field collection?

Thanks!

Cindy Meister MVP on Fri, 25 Jul 2014 15:38:55


Hi TilSol

If you look carefully at the field information that makes up the caption you should see that, for Heading numbering + caption number, there are TWO fields, a STyleREf and a SEQ field. The hyphen you're missing is BETWEEN the two fields, as static text. My tests (Word 2010) show that this is ANSI 30, a symbol Word shows as a hyphen but is not picked up in a "plain text" string.

The following combination returns 30, so I suppose you need to check which character code this returns (Select Case, perhaps) and use the information to  complete the string your code returns.

Asc(ActiveDocument.Range( _
    ActiveDocument.Fields(2).Result.End, _
    ActiveDocument.Fields(3).Code.Start).Text)
The other symbols used as separators (period, etc.) seem to return characters in the "normal" code range (32 onwards).