vba - 在 MS-Word VBA 中使用 APPLICATION.QUIT 但应用程序立即重新打开
问题描述
期望的行为:当下面的代码完成时,MS Word 应该关闭并保持关闭状态。
问题:问题是 MS Word 应用程序重新启动并打开“Instructions.docx”,其中包含指向 Normal.dotm 的 Module1 中的以下 VBA Sub 的宏按钮。
仅供参考:(1)我第一次发帖。(2) 在我进行一些 Windows 10 和 Office 365 更新之前,大约六个月没有出现这个问题。(3) Office 365 MSO (16.0.12527.20260) 32 位的 MS Word。(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:\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.
推荐阅读
- android - 在 onMessageReceived() 中检查通知的有效负载
- powerbi - 有没有办法在 DAX/Power BI 中创建可重用函数,将计算度量作为参数?
- php - 是否可以可视化电子邮件客户端的 HTML 更改?
- javascript - 获取带有 chrome 扩展名的选项卡图标并将其发送到 Flask [JS] [Python]
- java - (Java)如何创建一个循环,将我从数组生成的随机名称分配给多个字符串?
- ushort - 打开大文件并将其变成 ushort ?现有代码问题
- javascript - 使用 Dicer 将异步数据部分排序到文件中
- snowflake-cloud-data-platform - 找不到用户定义的函数
- google-apps-script - 如何以编程方式在 Google 文档中的特定位置添加表格?
- html - 如何使小 div 上的边框半径与大 div 相同