在文档末尾停止循环

发布于 2024-12-12 04:21:46 字数 1672 浏览 0 评论 0原文

我正在 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

I am programming a macro in VBA for MS Word to copy highlighted terms and paste them into another document. The problem is that I cannot stop the loop at the end of the document, and it continues the search from the beginning of the document and never stops.

Could someone help me please?

Here is the code of the macro:

  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆依然 2024-12-19 04:21:46

问题是:

.Wrap = wdFindContinue

这告诉它从头开始继续。

Here is the problem:

.Wrap = wdFindContinue

This tells it to continue again from the beginning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文