在文档末尾停止循环
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是:
这告诉它从头开始继续。
Here is the problem:
This tells it to continue again from the beginning.