I need to link my in-text citations to their corresponding entries in the bibliography list (IEEE style, e.g. [1], [2], [3], etc.).
Surprisingly, this is not something that is automated when exporting to PDF (as with footnotes and endnotes) when using the bibliography tool in Word.
So, I'm trying out macros/VBA to automate this for me, as I work with documents with 50+ references that need hyperlinks.
I'm a total newbie, but I'm trying to learn.
First, I have a macro that turns the in-text citation labels ([1], [2], etc.) to static text. This works fine, and enables me to edit the in-text citations before exporting to PDF. However, the most time consuming work is to link up the citations one by one.
So, after making the citations static text, I want a macro that does the following:
- Set the range to the bibliography section (section 4)
- Search the range for any numbers in brackets ([1], [2], etc.)
- Once a number in brackets are found, create a bookmark for that number, where the name of the bookmark is "Reference"+number. Do this for all the numbers in brackets in the given range (section 4).
Then, I want the macro (or another macro) to
- set the range to section 2 and 3 (the main document text)
- search the range for any numbers in brackets ([1], [2], etc.)
- once a number in brackets are found, insert a hyperlink in the number and brackets, where the hyperlink adress is the corresponding bookmark created earlier (e.g. [1] = "Reference1" bookmark, [2] = "Reference2" bookmark etc.)
Could anyone give me some guidance (or correct my code) to help with what I want to achieve?
This is my VBA code as of now, and I cannot get it to function. I run it, but nothing happens - it seems like it won't run, but I get no errors:
Sub CreateBookmarksAndHyperlinks()
' Set the range to bibliography section (section 4)
Dim biblioRange As Range
Set biblioRange = ActiveDocument.Sections(4).Range
' Search for numbers in brackets and create bookmarks
With biblioRange.Find
.ClearFormatting
.Text = "\[[0-9]+\]" ' Search for [1], [2], etc.
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
Dim foundTextBiblio As String
foundTextBiblio = Mid(biblioRange.Text, 2, Len(biblioRange.Text) - 2) ' Extract number between brackets
biblioRange.Bookmarks.Add "Reference" & foundTextBiblio, biblioRange
Loop
End With
' Set the range to main document text (sections 2 and 3)
Dim mainRange As Range
Set mainRange = ActiveDocument.Range(Start:=ActiveDocument.Sections(2).Range.Start, End:=ActiveDocument.Sections(3).Range.End)
' Search for numbers in brackets and insert hyperlinks
With mainRange.Find
.ClearFormatting
.Text = "\[[0-9]+\]" ' Search for [1], [2], etc.
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
Dim foundTextMain As String
foundTextMain = Mid(mainRange.Text, 2, Len(mainRange.Text) - 2) ' Extract number between brackets
If mainRange.Bookmarks.Exists("Reference" & foundTextMain) Then
' Insert hyperlink using the corresponding bookmark
ActiveDocument.Hyperlinks.Add Anchor:=mainRange, Address:="", SubAddress:="Reference" & foundTextMain, TextToDisplay:=mainRange.Text
End If
Loop
End With
End Sub
@
stands for one or more chars in Word searching (@jonsson comments it before my answer)mainRange.Bookmarks.Exists
should beActiveDocument.Bookmarks.Exists
Hyperlinks.Add
collapsesmainRange
to its start, addingmainRange.Move
to move the range.