Share via

How can I find and replace index entry instances "{.XE."keyword".}" with text?

Anonymous
2025-05-07T10:04:24+00:00

Background issue: I need to transform a 600-page Word document with hundreds of index markers into another format (LaTeX). There is some software that does this, but all ignore the index instances and only output the index at the end in a non-dynamic format (i.e., the page numbers in the index are purely textual and don't update).

My idea on how to fix this (any other suggestions are appreciated): I need to find a way to make all entries visible for textual search and replace them with the appropriate commands. Using the search function can find all the instances with "^d" in the search field, but this doesn't help. One needs to do the following: 1) find the hidden "{.XE."keyword".}" 2) replace it with "\index{keyword}" in text format.

I guess one needs a macro to do this... but I have no experience with macros. So, the question is whether there is a macro or another way to do this?

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

9 answers

Sort by: Most helpful
  1. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2025-05-07T22:44:58+00:00

    Here's a simpler approach that doesn't require you to know anything about the index entries:

    Application.ScreenUpdating = False
    
    Dim i As Long, Rng As Range, StrTxt As String
    
    With ActiveDocument
    
      For i = .Fields.Count To 1 Step -1
    
        With .Fields(i)
    
          If .Type = wdFieldIndexEntry Then
    
            Set Rng = .Code: StrTxt = Split(.Code.Text, Chr(34))(1)
    
            With Rng
    
              Do While .Characters.First <> Chr(19): .Start = .Start - 1: Loop
    
            End With
    
            .Delete: Rng.Text = "\index{" & StrTxt & "}"
    
          End If
    
        End With
    
      Next
    
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2025-05-07T11:17:51+00:00

    All you need do is a Find/Replace, where;

    Find = ^d XE keyword

    Replace = \index{keyword}

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2025-05-07T11:51:02+00:00

    Unless you have many different index keywords, the overall number is of little consequence.

    For many different index keywords, you could use a macro like:

    Sub BulkIndexConverter()
    
    Application.ScreenUpdating = False 
    
    Dim i As Long, StrFnd As String, ArrFnd() As String 
    
    StrFnd = ",keyword1,keyword2,keyword3,keyword4,keyword5,keyword6,keyword7,keyword9,keyword1,keyword9" & _ 
    
      ",keyword10,keyword11,keyword12,keyword13,keyword14,keyword15,keyword16,keyword17,keyword18,keyword19" & _ 
    
      ",keyword20,keyword21,keyword22,keyword23,keyword24,keyword25,keyword26,keyword27,keyword28,keyword29" 
    
    ArrFnd() = Split(StrFnd, ",") 
    
    With ActiveDocument.Range.Find 
    
      .ClearFormatting 
    
      .Replacement.ClearFormatting 
    
      For i = 1 To UBound(ArrFnd) 
    
        .Text = "^d XE " & ArrFnd(i) 
    
        .Replacement.Text = "\index{" & ArrFnd(i) & "}" 
    
        .Execute Replace:=wdReplaceAll 
    
      Next 
    
    End With 
    
    Application.ScreenUpdating = True 
    
    End Sub
    

    Just make sure that, if any keyword is the starts a longer keyword, you process the longer one first. For example, you would need to process 'greenhouse' before processing 'green'.

    For PC macro installation & usage instructions, see: http://www.gmayor.com/installing\_macro.htm

    For Mac macro installation & usage instructions, see: https://wordmvp.com/Mac/InstallMacro.html

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2025-05-07T11:20:45+00:00

    As I said, there are hundreds of keywords. Not practical.

    Was this answer helpful?

    0 comments No comments
  5. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more