在MS Word VBA中使用APPLICATION.QUIT,但应用程序立即重新打开



所需行为:当下面的代码完成时,MS Word应该关闭并保持关闭状态。

问题:问题是MS Word应用程序重新启动并打开"Instructions.docx",其中有一个宏按钮指向Normal.dotm.的Module1中的以下VBA Sub

财政年度:(1( 我第一次发帖。(2( 这个问题没有发生大约六个月,之前我采取了一些Windows10&Office 365更新。(3( MS Word for Office 365 MSO(16.0.12527.20260(32位。(4( Windows 10 Pro,64位操作系统,i5-6500@2.5 GHz,8 GB RAM。

问题:当我的Normal.dotm VBA关闭时,如何使MS Word应用程序保持关闭状态?

Sub Finishing()
'To reset and save Instruction.docx
'To move existing Outputs folder to a new date-specific destination
'To close all open *.docx's then quit MS Word application
Dim TodaysYear, FridaysDate, ToPath, FromPath As String
'(1) Reset_All_Checkboxes as adapted from Greg Maxey
Dim oCC As ContentControl
For Each oCC In ActiveDocument.Range.ContentControls
oCC.Checked = False
Next
'(2) Move_and_Rename_Outputs_Dir as adapted from Ron de Bruin
ToPath = "C:UsersmeOneDriveDocumentsTrade Records" & Format(Date, "yyyy") & "" & Format(DateAdd("d", 4, Date), "yyyy-mm-dd") 'add 4 days
FromPath = "C:UsersmeOneDriveDocumentsOutputs"
Dim FSO As Object
Set FSO = CreateObject("scripting.filesystemobject")
FSO.MoveFolder Source:=FromPath, Destination:=ToPath
'(3) Save and close all open files then quit Word
With Application
.ScreenUpdating = False
'Loop through open documents
Do Until .Documents.Count = 0
.Documents(1).Close SaveChanges:=wdSaveChanges
Loop
.Quit SaveChanges:=wdSaveChanges 'Quit MS Word
End With
End Sub

通过将第3节修改为以下内容来修复:

'(3) Save Instructions.docx without prompts then prompt to close all open files then quit Word
'This achieves Desired Behavior: Instructions.docs exit-macro runs without problem of re-opening MS Word application after the Word macro closes the Instructions document & exits the Word application
Documents("Instructions .docx").Activate
ActiveDocument.Save 'if Instructions.docx is the only open Word doc, macro completes without prompt
Application.Quit SaveChanges:=wdDoNotSaveChanges 'This saves option is for changes to Normal.dotm. Other Save Options are wdSaveChanges & wdPromptToSaveChanges.

相关内容

最新更新