在论坛用户的帮助下,我能够完美地执行以下代码。 然而,当我想保存并关闭word程序时,却提示如下一行:
正在保存的文档包含跟踪的更改。继续保存吗?
有没有办法在使用VBA保存word文档时抑制所有提示和警告?
我的工作代码:
Sub DocSearchandReplace()
Dim wdApp As Object, wdDoc As Object
Set wdApp = CreateObject("word.application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open("P:\test.doc")
With wdDoc.Content.Find
.ClearFormatting
.Text = "[0-9]{2}.[0-9]{2}.[0-9]{4}"
.MatchWildcards = True
.Replacement.ClearFormatting
.Replacement.Text = ThisWorkbook.Worksheets("sheet1").Range("A1").Text
.Execute Replace:=2, Forward:=True, Wrap:=1
End With
wdDoc.Save 'it prompts and asks me whether continue to save
wdApp.ActiveDocument.PrintOut 'here again the same prompt when executing this line.
wdApp.Quit
Set wdApp = Nothing: Set wdDoc = Nothing
End Sub
wdApp.Options.WarnBeforeSavingPrintingSendingMarkup = false
如果您想禁用保存提示,您可以这样做:
wdApp.ActiveDocument.Saved = True
wdApp.ActiveDocument.SaveAs (path & newFile)
记住保存提示不能仅通过此代码禁用:
Application.DisplayAlerts=false
您也可以使用此 VBA 代码:
Documents.Save NoPrompt:=True, _
OriginalFormat:=wdOriginalDocumentFormat