cross-references according to GOST (we remove from the text the spurious names “Figure..”, “Table..”, etc.)

I was asked to write an article about the known problem with spurious cross-reference titles in the text of a MS Word document, namely, when you insert a cross-reference into the document text (link menu / cross-reference), Word allows you to insert either a title like “Figure …” or the name in full, which leads to texts like “…in the figure (Figure 10) we see…” or even more ridiculous designs, whereas according to GOST, it is necessary to leave only the number “… in Figure 10 we see.. “.

This problem can be easily solved with a small VBA script.

The cross-reference code (you can view it by switching viewing modes “shift + f9”) looks something like this { REF _Ref127884797 \h } In order to remove spurious names and leave only the number, you need to add the key “\#0“. At the same time, you need to make sure that unnecessary keys are not accidentally inserted (this will lead to errors) and correctly switch the display of the cross-reference from the “text” view to the “code” view and back.

Below is the code that does this.

Sub PerSsylkiGost()

Dim I As Long, DlStroki As Long, LinkText$
    'Перебираем все поля типа ссылки
    For I = 1 To ActiveDocument.Fields.Count
           'Выбираем из них перекрёстные ссылки и выделяем их
        If ActiveDocument.Fields.Item(I).Type = wdFieldRef Then
            With Selection
                LinkText = .Text
'   MsgBox Mid(LinkText, 2, 4)
                 'Проверка на корректность открытия кода ссылки, т.к. в тексте могут
           'присутствовать п.ссылки в виде кода и текста
           'приводим п.ссылку к виду "код"
                If Len(LinkText) < 4 Then
                    If Mid(LinkText, 2, 4) = "Ref " Or Mid(LinkText, 2, 4) = " Ref" _
                    Or Mid(LinkText, 2, 4) = "REF " Or Mid(LinkText, 2, 4) = " REF" Then
                    End If
                End If
                  'содержаение кода п.ссылки передаём переменной и обрезаем её с краёв  
                 LinkText = .Text
                 DlStroki = Len(LinkText)
                 LinkText = Mid(LinkText, 2, DlStroki - 2)
                 'Проверяем, не содержит ли уже п.ссылка нужный нам код - во избежание ошибок 
                If InStr(LinkText, "# 0") <> 0 Or InStr(LinkText, "#0") <> 0 Then
                    With .Find
                  'Если кода в п.ссылке нет, вставляем его   
                            .Text = LinkText
                            .Replacement.Text = LinkText & "\#0 "
                            .Forward = True
                            .Wrap = wdFindStop
                            .Format = False
                            .MatchCase = False
                            .MatchWholeWord = False
                            .MatchWildcards = False
                            .MatchSoundsLike = False
                            .MatchAllWordForms = False
                            .Execute Replace:=wdReplaceAll
                    End With
                 'Обновляем п.ссылки - приводим их к нормальному виду        
                End If
            End With

        End If
    Next I

End Sub

It would seem to be a very simple task, but for some reason Word does not allow you to solve it using standard tools…

Additional checks on the type and content of the cross-reference code are necessary, because It often happens that a document is composed of pieces of other documents in which these links may be in different states.

Use it))) Good luck!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *