简体   繁体   English

如何从 MS Excel vba 禁用或延迟 MS Word 中的自动保存

[英]How to disable or delay autosave in MS Word from MS Excel vba

I have an Excel file that exports a large amount of data to Word (about 100 pages) and I'm trying to turn off autosave during the export to speed it up.我有一个将大量数据导出到 Word(大约 100 页)的 Excel 文件,我试图在导出过程中关闭自动保存以加快速度。 I've tried two methods so far but neither seems to do what I expected: (note that the code is in the excel file from which the data is exported)到目前为止,我已经尝试了两种方法,但似乎都没有达到我的预期:(请注意,代码位于导出数据的 excel 文件中)

First method:第一种方法:

oWordfile.AutoSaveOn = False   'oWordfile is type Word.Document
MsgBox "Autosave state: " & oWordfile.AutoSaveOn

I used the msgbox periodically within the code to confirm the autosave state hadn't been changed.我在代码中定期使用 msgbox 来确认自动保存状态没有改变。 The title bar of the Word file I am exporting too indicates it is saving about every 5 minutes while the msgbox popups always indicate that the autosave state is False.我正在导出的 Word 文件的标题栏也表明它大约每 5 分钟保存一次,而 msgbox 弹出窗口始终表明自动保存状态为 False。

Second method:第二种方法:

oWordApp.Options.SaveInterval = 30    'oWordApp is type Word.Application

After code execution, I confirmed that the AutoRecover save interval had been changed to 30 minutes, but during export, the file still saves about every 5 minutes.执行代码后,我确认 AutoRecover 保存间隔已更改为 30 分钟,但在导出过程中,文件仍然大约每 5 分钟保存一次。

Since neither of these two methods changed the behavior of Word, I figure I must be changing the wrong setting but I'm not sure what other settings control the autosave feature.由于这两种方法都没有改变 Word 的行为,我想我一定是更改了错误的设置,但我不确定还有哪些其他设置控制自动保存功能。 If any one knows what I'm missing, I'd be grateful for the help.如果有人知道我缺少什么,我将不胜感激。

updates after Byrd's answer:伯德回答后的更新:

Third method: cancel save:第三种方法:取消保存:

MS-Word MS-Word

Private WithEvents App As Word.Application

Private Sub Document_Open()
  Set App = Word.Application
End Sub

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
  If MsgBox("Save Document?", vbYesNo, "Word: BeforeSave") = vbNo Then
    Cancel = True
  End If
End Sub

Excel Excel

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  If MsgBox("Save File?", vbYesNo, "Excel: BeforeSave") = vbNo Then
    Cancel = True
  End If
End Sub

I used message boxes as a temporary solution as to when a save is wanted to test this method.我使用消息框作为临时解决方案,以了解何时需要保存来测试此方法。 The message boxes appear any time the code or user initiates a save event in Excel and Word.只要代码或用户在 Excel 和 Word 中启动保存事件,就会出现消息框。 If the word file is left open the message box also appears when autorecover save event triggers (currently set to 30 minutes).如果 word 文件保持打开状态,则在触发自动恢复保存事件时也会出现消息框(当前设置为 30 分钟)。 During code execution exporting from Excel to Word, Word still saves at 5-minute intervals without triggering the BeforeSave event.在从 Excel 导出到 Word 的代码执行期间,Word 仍以 5 分钟的间隔保存,而不会触发 BeforeSave 事件。

指示保存的单词标题栏

指示文件已保存的 Word 标题栏

I have now tested a little further and discovered that even though the title bar indicates saving is happening during the export, the file hasn't actually been saved.我现在进行了更进一步的测试,发现即使标题栏指示在导出过程中正在进行保存,文件实际上并没有被保存。 Using task manager to force close Word directly after export, I discovered that the file was unchanged from before code execution.导出后直接使用任务管理器强制关闭Word,发现文件与代码执行前相比没有变化。 Autorecover didn't have a version of the file to recover either. Autorecover 也没有要恢复的文件版本。 So now I have no idea of what is actually happening - all I know is that the export slows down greatly while the title bar indicates a save is in progress.所以现在我不知道实际发生了什么——我只知道导出速度大大减慢,而标题栏显示正在进行保存。

Excel Excel

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Cancel = True

End Sub

Word单词

Private Sub appWord_DocumentBeforeSave _ 
 (ByVal Doc As Document, _ 
 SaveAsUI As Boolean, _ 
 Cancel As Boolean) 

 Cancel = True

End Sub

I don't know if the user will need to save the document at all during this export but, just canceling the save all together might do it.我不知道用户在此导出过程中是否需要保存文档,但是,只需取消全部保存即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM