逐个单词进行搜索并创建表格的宏

逐个单词进行搜索并创建表格的宏

你好,我有这个 Word 宏,它应该扫描一个 Word 文档,找到单词“命令”,然后将该单词转换为表格,其中命令在第一列,结果在第二列。我做错了什么?

Sub Search()
Dim IsFound As Boolean
IsFound = True

Selection.Find.ClearFormatting
With Selection.Find
    .Text = "Command:"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

While IsFound
 Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator, _
NumColumns:=2, NumRows:=8, AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
    .Style = "Table Grid"
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = False
    .ApplyStyleLastColumn = False
IsFound = Selection.Find.Execute

End With
Wend
End Sub

预先感谢您的任何帮助

答案1

试试这个...希望它有帮助。

Sub Search()
Selection.HomeKey Word.WdUnits.wdStory
Selection.Find.ClearFormatting
With Selection.Find
  .Text = "Command:"
  .Replacement.Text = ""
  .Forward = True
  .Wrap = wdFindStop
  .Format = False
  .Execute
Do While .Found
    Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator, _
        NumColumns:=2, NumRows:=8, AutoFitBehavior:=wdAutoFitFixed
    With Selection.Tables(1)
        .Style = "Table Grid"
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleLastColumn = False
    End With
    Selection.Collapse Word.WdCollapseDirection.wdCollapseEnd
    .Execute
Loop
End With
End Sub

相关内容