Skip to content

Instantly share code, notes, and snippets.

@worldbeater
Last active March 8, 2024 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save worldbeater/67b2e9c2ef3417e65a1ac6c1a6c9f5d7 to your computer and use it in GitHub Desktop.
Save worldbeater/67b2e9c2ef3417e65a1ac6c1a6c9f5d7 to your computer and use it in GitHub Desktop.
A Visual Basic for Applications (VBA) code for finding all references (e.g. [42], [2—3, 4]) and increasing their numbers based on the preconfigured N and M values. Useful when inserting a new reference into the beginning of the bibliography of a large DOCX.
Sub Highlight(Phrase As String)
Application.ScreenUpdating = False
With ActiveDocument
With .Range
With .Find
.ClearFormatting
.replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchCase = True
.MatchWholeWord = True
.Text = Phrase
.replacement.Text = ""
End With
Do While .Find.Execute
.Text = Replace(Replace(Phrase, "[", "`"), "]", "|")
.HighlightColorIndex = wdBrightGreen
.Collapse wdCollapseEnd
Loop
End With
End With
Application.ScreenUpdating = True
End Sub
Sub HighlightReferences()
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\[([1-9][0-9, —-]*)\]"
regex.IgnoreCase = True
regex.Global = True
Set matches = regex.Execute(ActiveDocument.Content.Text)
For j = matches.Count - 1 To 0 Step -1
Debug.Print matches(j)
Highlight (matches(j))
Next
End Sub
Sub ReplaceReference(Phrase As String, replacement As String)
pinn = Mid(Phrase, 2, Len(Phrase) - 2)
rinn = Mid(replacement, 2, Len(replacement) - 2)
Application.ScreenUpdating = False
With ActiveDocument
With .Range
With .Find
.ClearFormatting
.replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchCase = True
.MatchWholeWord = True
.Text = Phrase
.replacement.Text = ""
End With
Do While .Find.Execute
.Text = replacement
.HighlightColorIndex = IIf(StrComp(pinn, rinn), wdTurquoise, wdPink)
.Collapse wdCollapseEnd
Loop
End With
End With
Application.ScreenUpdating = True
End Sub
Function ComputeReplacement(Reference As String) As String
M = 208 ' The number of the first newly added reference to the bibliography section.
N = 1 ' The count of the newly added references to the bibliography section.
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "[0-9]+"
regex.IgnoreCase = True
regex.Global = True
Value = Replace(Replace(Reference, "`", ""), "|", "")
Set matches = regex.Execute(Value)
For j = matches.Count - 1 To 0 Step -1
match = matches(j)
Number = CInt(match)
If Number >= M Then
Number = Number + N
End If
Value = Replace(Value, match, CStr(Number), 1, 1)
Next
Value = Replace(Value, "-", "—")
ComputeReplacement = "[" + Value + "]"
End Function
Sub ReplaceReferences()
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\`([1-9][0-9, —-]*)\|"
regex.IgnoreCase = True
regex.Global = True
Set matches = regex.Execute(ActiveDocument.Content.Text)
For j = matches.Count - 1 To 0 Step -1
Dim repl As String
Dim match As String
match = matches(j)
repl = ComputeReplacement(match)
Debug.Print match
ReplaceReference match, repl
Next
End Sub
Sub Op()
HighlightReferences
ReplaceReferences
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment