是否可以跳过错误“文档上次打开时发生严重错误”?我使用 Excel 打开 Word 文件。我想防止出现这种情况,例如电脑在运行宏时重新启动,后来Word认为文件有问题。但我始终确信我的模板文件没问题(我使用只读方法)。跳过意味着用 VBA 选择“是”继续打开文件。将显示警报设置为 false 不起作用。谢谢!
Set wApp = CreateObject("Word.Application")
With wApp
.DisplayAlerts = False
.Visible = False
.ScreenUpdating = False
End With
Edit:
我刚刚发现唯一的方法似乎是编辑注册表:如何以编程方式重新启用 MS Office 禁用文件列表中的文档 https://stackoverflow.com/questions/751048/how-to-programatically-re-enable-documents-in-the-ms-office-list-of-disabled-fil不幸的是,由于公司的IT安全管理,我无法通过宏修改注册表。
Edit2:
我在下面发布了解决方法
我发现解决问题的方法并不优雅,但确实很简单。在Windows注册表中有一个“黑名单”(称为禁用列表)包含 Office 上次打开时发现有问题的文件。解决方法是暂时更改文件名,而不是删除系统注册表中的条目。
我认为初学者的示例代码比描述更清晰:
' folder's path
Dim fPath As String: fPath = "D:\templates\"
' Word file to open
Dim fName As String: fName = "sample.docx"
Dim fullFilePath As String
fullFilePath = fPath & fName
' temporary name
' I use here idea to add unique timestamp prefix
Dim tempName As String
tempName = VBA.Strings.Format(Now(), "yyMMdd_hhmmss_") & fName
Dim fullTempFilePath As String
fullTempFilePath = fPath & tempName
' temporary change
Name fullFilePath As fullTempFilePath
Dim wApp as object ' late binding
Set wApp = CreateObject("Word.Application")
Set doc = wApp.Documents.Open(Filename:=fullTempFilePath)
' here some code to file manipulate
' ...
doc.Close
' restore previous file name
Name fullTempFilePath As fullFilePath
' end
wApp.Quit
Set wApp = Nothing
我会添加一些错误处理以确保恢复以前的文件名。如果有人看到更好的想法,请随时在这里分享。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)