MS Word宏以查找文本引文

问题描述 投票:-2回答:1

我处理包含手动添加引用的超大文档。我的任务之一是检查文档末尾所有参考文献是否都已包含在参考书目中。这些文本引用的格式如下:

  1. (Tom等,2001)
  2. [(Dick and Harry 2007)
  3. [(Jack等,2009; Jill等,2011)。

我已经尝试编写一个宏,该宏在我的文档中搜索上面列出的格式的文本,然后在新的Word文档中列出它们。理想情况下,我想将此宏仅应用于当前选定的文本,而不是整个文档。

我已经编写了一个可在宏中使用的正则表达式搜索字符串:

\(*[0-9]{4}\)

但是给出测试字符串:

[(ABC)狐狸跳过那只狗(Tom等,2001)。

我得到的结果是:

[(ABC)狐狸跳过那只狗(Tom等,2001)

我想获得“ Tom等,2001”或“(Tom等,2001)”。

**任何人都可以改善我的搜索字符串或提出更好的方法吗?

我的整个宏如下:

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

提前感谢您的帮助!

word-vba
1个回答
0
投票

更改:

.Text = "\(*[0-9]{4}\)"

至:

.Text = "\([!\)]@[0-9]{4}\)"
© www.soinside.com 2019 - 2024. All rights reserved.