[Solved] Type Mismatch error showing for name of macro in vb editor - using vba to insert a quotation mark

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

[Solved] Type Mismatch error showing for name of macro in vb editor - using vba to insert a quotation mark

Post by Charles Kenyon »

:grin: NEVERMIND --- SOLVED -- solution below


I am trying to write a macro to replace all instances of an Index entry with an expanded entry. What I've written so far is:

Code: Select all

Sub IndexEntryFieldModify()
    'A basic Word macro coded by Charles Kenyon - based on Greg Maxey format
    '
    '   DECLARE VARIABLES / CONSTANTS
    Dim oRng As range
    Dim oFld As Field
    Dim iCount As Long
    Dim i As Long
'================================================
    ' declare old and new entries - change to suit
    Const strOld = """Einstein"""
    Const strNew = """Einstein, Albert"""
'================================================
    '   ACTIONS
    With ActiveDocument
        Let iCount = .Fields.Count
        For i = 1 To iCount
            If .Fields(i).Type = wdFieldIndexEntry Then
                If .Fields(i).Code = " XE " & strOld Then .Fields(i).Code = " XE " & strNew
            End If
        Next i
    End With
    On Error GoTo lbl_Exit
    '   EXIT PROCEDURE
lbl_Exit:
    '   CLEAR ERROR HANDLER AND OBJECTS
    On Error GoTo -1
    Set oRng = Nothing
    Set oFld = Nothing
    Exit Sub
End Sub
Problem 1: When I attempt to run this, I get a Type Mismatch error and the debugger points to the first line with the name of the macro. This may be related to Problem 2.

Problem 2: I am trying to insert text inside the field code that has quotation marks as part of the text. I do not know if I am doing this correctly because I can't even get it to start running. What I am using is a triple quotation mark (""") where I want this. It occurs to me that trying to read "Einstein" I may need """ & "Einstein" & """ or something completely different.
[*]
Last edited by Charles Kenyon on 12 Feb 2023, 19:25, edited 4 times in total.

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Type Mismatch error showing for name of macro in vb editor - using vba to insert a quotation mark

Post by Charles Kenyon »

I discovered that I want .Fields(i).Code.Text.
Still working on it.

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Type Mismatch error showing for name of macro in vb editor - using vba to insert a quotation mark

Post by Charles Kenyon »

Here is the repaired code.
The Type Mismatch error was caused by trying to write to .Fields(i).Code rather than .Fields(i).Code.Text.
The way to get the quotation mark was Chr(34).

Code: Select all

Sub IndexEntryFieldModify() '
    ' A scratch Word macro coded by Charles Kenyon - based on Greg Maxey format
    ' 2023-02-09
    ' https://answers.microsoft.com/en-us/msoffice/forum/all/how-do-i-edit-reconfigure-index-entries/f452ce26-d802-4f55-86e7-31231d2db065
    
    '   DECLARE VARIABLES / CONSTANTS
    Dim iCount As Long
    Dim i As Long
'================================================
    ' declare old and new entries - change to suit
    Const strOld As String = "Einstein"
    Const strNew As String = "Einstein, Albert"
'================================================
    '   ACTIONS
    With ActiveDocument
        Let iCount = .Fields.Count
        For i = 1 To iCount
            If .Fields(i).Type = wdFieldIndexEntry Then
                If .Fields(i).Code.Text = " XE " & Chr(34) & strOld & Chr(34) & " " Then
                    .Fields(i).Code.Text = " XE " & Chr(34) & strNew & Chr(34) & ""
                End If
            End If
        Next i
    End With
End Sub