在 MS-Word VBA 中使用 APPLICATION.QUIT 但应用程序立即重新打开
Using APPLICATION.QUIT in MS-Word VBA but Application Immediately Re-opens
期望的行为:
当下面的代码完成时,MS Word 应该关闭并保持关闭状态。
问题:
问题是 MS Word 应用程序重新启动并打开 "Instructions.docx,",其中的宏按钮指向 Normal.dotm.
模块 1 中下面的 VBA 子项
仅供参考:
(1) 第一次发帖。
(2) 在我进行一些 Windows 10 和 Office 365 更新之前,这个问题大约六个月没有发生。
(3) MS Word for Office 365 MSO (16.0.12527.20260) 32 位。
(4) Windows 10 Pro,64 位 OS,i5-6500 @ 2.5 GHz,8 GB 内存。
问题:
当我的 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:\Users\me\OneDrive\Documents\Trade Records\" & Format(Date, "yyyy") & "\" & Format(DateAdd("d", 4, Date), "yyyy-mm-dd") 'add 4 days
FromPath = "C:\Users\me\OneDrive\Documents\Outputs"
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.
期望的行为: 当下面的代码完成时,MS Word 应该关闭并保持关闭状态。
问题: 问题是 MS Word 应用程序重新启动并打开 "Instructions.docx,",其中的宏按钮指向 Normal.dotm.
模块 1 中下面的 VBA 子项仅供参考: (1) 第一次发帖。 (2) 在我进行一些 Windows 10 和 Office 365 更新之前,这个问题大约六个月没有发生。 (3) MS Word for Office 365 MSO (16.0.12527.20260) 32 位。 (4) Windows 10 Pro,64 位 OS,i5-6500 @ 2.5 GHz,8 GB 内存。
问题: 当我的 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:\Users\me\OneDrive\Documents\Trade Records\" & Format(Date, "yyyy") & "\" & Format(DateAdd("d", 4, Date), "yyyy-mm-dd") 'add 4 days
FromPath = "C:\Users\me\OneDrive\Documents\Outputs"
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.