Reputation: 107
I work with very large documents that contain manually added citations. One of my tasks is to check if all the citations have been included in the bibliography at the end of the document. These in-text citations come in the following format:
I tried to write a macro that searches my document for text in the above listed formats and then lists them in a new Word document. I'd like to apply this macro to the currently selected text instead of the whole document.
I have written a regular expression search string to use in the macro:
\(*[0-9]{4}\)
But given the test string:
The (ABC) fox jumps over the dog (Tom et al. 2001).
I get a result of:
(ABC) fox jumps over the dog (Tom et al. 2001)
I would like to get "Tom et al. 2001" or "(Tom et al. 2001)".
Can improve my search string or approach be improved?
My entire macro is as follows:
Sub ExtractRefsFromSelection()
MsgBox ("This macro extracts references from selected text.")
Dim SearchRange As Range, DestinationDoc$, SourceDoc$
DestinationDoc$ = "Refs.doc"
SourceDoc$ = ActiveDocument.Name
Documents.Add DocumentType:=wdNewBlankDocument
ActiveDocument.SaveAs DestinationDoc$, wdFormatDocument
Documents(SourceDoc$).Activate
Set SearchRange = ActiveDocument.Range
With SearchRange.Find
.ClearFormatting
.Text = "\(*[0-9]{4}\)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
While .Execute
Documents(DestinationDoc$).Range.Text = Documents(DestinationDoc$).Range.Text + SearchRange.Text
Wend
End With
End Sub
Upvotes: 0
Views: 1271
Reputation: 13515
Change:
.Text = "\(*[0-9]{4}\)"
to:
.Text = "\([!\)]@[0-9]{4}\)"
Upvotes: 1