Error 4198 when inserting a cross reference in a Word document using VBA

Anonymous
2017-07-10T08:48:28+00:00

Hi there,

I am facing problems when inserting some cross references in a Word document using VBA. This is a weird thing as the code works fine for most of the headings but it fails randomly with some of them. 

It seems an issue with some "hidden characters" on the heading that provoke a conflict with the VBA instruction. I also tried to record a macro but it also fails with the same error (Err.code=4198) when running the generated code. The weird thing is that from the Word menu I can insert the cross reference without any problems but when executing the code it fails. 

How could I know what if wrong within the document?. 

The code I am using to insert the cross reference is the following:

Selection.InsertCrossReference ReferenceType:=wdRefTypeHeading, ReferenceKind:=wdNumberNoContext, ReferenceItem:=CStr(iHeadingIndex), InsertAsHyperlink:=True, SeparateNumbers:=False 

I'd appreciate some help with this. Thank you very much in advance.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2017-07-12T08:50:57+00:00

    I post here the solution as Doug has finally found it. The issue was related with "hidden" headings from previous changes on the document that were not accepted. So when inserting a cross reference to an index of the Headings array the macro failed.

    I could fix it manually just selecting Track Changes : Original Showing Markup 

    And then accepting all changes

    By code I modified the macro so from now on it makes this automatically: 

    ' Track changes : Original Showing Markup

    With ActiveWindow.View

            .RevisionsView = wdRevisionsViewOriginal

            .RevisionsView = wdRevisionsMarkupAll

    End With

    ' Accept all changes in document

    ActiveDocument.Revisions.AcceptAll

    Thanks a lot Doug for your help!

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 322.1K Reputation points MVP Volunteer Moderator
    2017-07-10T09:26:06+00:00

    Have you checked what is being returned by

    CStr(iHeadingIndex)

    0 comments No comments
  2. Anonymous
    2017-07-10T09:47:28+00:00

    Hi Doug,

    yes, I've checked it. iHeadingIndex contains an integer which references to the right index of the Headings array:

    MyHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)

    ' Find the corresponding index in MyHeadings to insert a Cross Reference

    i = 1

    Do While i <= UBound(MyHeadings)

        If Left(Trim(LCase(MyHeadings(i))), Len(XE.Section)) = XE.Section Then

            iHeadingIndex = i

           Exit Do 

        Else

            i = i + 1

        End If

    Loop

    This code works fine for most of the document references but it fails with a few of them and I can't figure out what's going on. I've read that there could be "extra spaces" before the headings but nothing works. 

    Thanks in advance.

    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 322.1K Reputation points MVP Volunteer Moderator
    2017-07-11T00:48:24+00:00

    If you send me a copy of the document and the code of the macro, referencing this thread in the covering email message, I will investigate the issue.

    0 comments No comments