【发布时间】: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
【问题讨论】: