boar24 / InsertCrossReferencesComfortably

VBA macro to comfortably insert cross references in MS Word
12 stars 1 forks source link

Interner Fehler error with some of the captions #4

Closed QvS1 closed 9 months ago

QvS1 commented 1 year ago

Hi, I really like your macro it should be a MS standard to do it this way.

However, on some captions the macro scrolls trough the whole document and gives the Interner Fehler error. This happens at captions created from image imported from Ppt to word. These captions are located in their own sort of text box.

is there a way to tweak/update the macro to accommodate for these kind of captions

Kind Regards,

QvS1

boar24 commented 1 year ago

Hi, glad you like it.

Can you please provide a file for me to download which shows the effect so that I can have a look at it.

Best regards

QvS1 commented 1 year ago

Hi, thanks for the reaction.

hereby an example file.

crossrefmacro_example.docx

Regards, QvS1

boar24 commented 9 months ago

Hi, thanks for the very helpful example file with which I could easily reproduce the effect.

I now had a look into the code to see what happens. It turned out that Microsoft's call .GetCrossReferenceItems() only presents the cross references (XRefs) which are plain inside the document body, but not those which are in those Textboxes. I tried a workaround (see below) but that didn't work either. I had to give up and will close the issue. I would recommend that you remove those textboxes if you want to use my macros. Sorry for not having better news. Best regards

        ' Regarding the issue that crossrefs are only found in the document body,
        ' but not if they are within Textboxes:
        '
        ' Microsoft says (https://learn.microsoft.com/en-gb/office/vba/api/word.selection.insertcrossreference)
        ' that <Selection.InsertCrossReference()> can be used with <ReferenceItem> where
        ' "this argument specifies the item number or name in the Reference type box in the Cross-reference dialog box".
        ' We have found out, that the dialog box
        ' - first lists the cross references in the document body (wdMainTextStory ?)
        ' - then  lists the cross references in the text boxes (wdTextFrameStory).
        ' This is true independent of the order of the elements in the document, so:
        ' first all Xrefs in the document, then all Xrefs in the TextFrames.
        '
        ' Idea therefore: if the XRef was not found in the document body, then search in the TextFrames.
        ' This is what the below code does. It finds the XRef and returns the index,
        ' i.e. the ordinal of the XRef within the TextFrame XRefs.
        ' Then if we add this ordinal to the number of XRefs in the document body,
        ' we exactly get the "item number [..] in the Cross-reference dialog box", i.e. the Index. Sounds good so far.
        ' However, when pass this Index to <Selection.InsertCrossReference()> to actually insert the XRef,
        ' then this function crashes, apparently believing that the given index is out-of-bounds.
        ' So there is currently no solution. :-(
QvS1 commented 9 months ago

Hi, Thanks for taking the time looking in to this query.

Cheers,

QvS1