停止在文档末尾循环

时间:2011-10-26 13:18:14

标签: vba loops ms-word word-vba

我在VBA中为MS Word编写宏来复制突出显示的术语并将其粘贴到另一个文档中。问题是我不能在文档的末尾停止循环,并且它从文档的开头继续搜索并且永远不会停止。

有人能帮帮我吗?

以下是宏的代码:

  Sub gethigh()
  '
  ' gethigh Macro
  '

  MsgBox ("Before processing the file, this macro is going to save it as: << sourcedoc.doc >> and to create another document called: << targetdoc.doc >> and will then perform the export. Click OK to continue.")

  ActiveDocument.SaveAs ("sourcedoc.doc")
  Documents.Add

  ActiveDocument.SaveAs "targetdoc.doc"
  Documents("sourcedoc.doc").Activate
  Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst
  Selection.Find.ClearFormatting
  Do
      Selection.Find.Highlight = True
      With Selection.Find
          .Text = ""
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindContinue
          .Format = True
      End With
      Selection.Find.Execute
      Selection.Copy
      Documents("targetdoc.doc").Activate
      Selection.GoTo What:=wdGoToLine, Which:=wdGoToLast
      Selection.PasteAndFormat wdPasteDefault
      Selection.Find.ClearFormatting
      Selection.InsertBreak Type:=wdLineBreak
      Documents("sourcedoc.doc").Activate
  Loop
  Documents("targetdoc.doc").Activate
  Selection.Find.ClearFormatting
  Selection.Find.Highlight = True
  Selection.Find.Replacement.ClearFormatting
  Selection.Find.Replacement.Highlight = False
  With Selection.Find
      .Text = ""
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindContinue
      .Format = True
  End With
  Selection.Find.Execute Replace:=wdReplaceAll
  Documents.Save noprompt:=True
  MsgBox "Processing is complete."
End Sub

1 个答案:

答案 0 :(得分:4)

问题在于:

.Wrap = wdFindContinue

这告诉它从头开始再次继续。

相关问题