【问题标题】:Stop looping at the end of a document在文档末尾停止循环
【发布时间】:2011-12-15 17:34:23
【问题描述】:

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

【问题讨论】:

    标签: vba loops ms-word


    【解决方案1】:

    问题来了:

    .Wrap = wdFindContinue
    

    这告诉它从头开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多