[英]Stop ms Access VBA opening Excel File Read-only
I have an Access file which uses tables linked to an Excel file, which in itself has other links that need refreshing.我有一个 Access 文件,它使用链接到 Excel 文件的表,该文件本身还有其他需要刷新的链接。
(Please please don't question the logic of that, it is a workaround for some knotty problems and genuinely the only way of doing it for now because access can't use.odc connections) (请不要质疑其中的逻辑,这是解决一些棘手问题的一种解决方法,并且是目前唯一的解决方法,因为访问无法使用.odc 连接)
I have a button in an access form that should update the data.我在访问表单中有一个应该更新数据的按钮。 However when the
button_onclick
sub opens Excel it always opens it as read-only, and therefore breaks when trying to save the file.但是,当
button_onclick
子打开 Excel 时,它始终以只读方式打开,因此在尝试保存文件时会中断。 How do I prevent this?我该如何防止这种情况?
I have already tried AppExcel.AlertBeforeOverwrite = False
and entering the ReadOnly
parameter as false on Workbook.Open
but it still happens.我已经尝试过
AppExcel.AlertBeforeOverwrite = False
并在Workbook.Open
上将ReadOnly
参数输入为 false,但它仍然会发生。
Code is as follows;代码如下;
Private Sub btnUpdate_Click()
Dim AppExcel As Excel.Application
Dim wbConn As Excel.Workbook
Set AppExcel = CreateObject("Excel.Application")
AppExcel.Visible = True
Set wbConn = AppExcel.Workbooks.Open("Z:\Company Records\System Files\Connection Data.xlsx", True, False) 'Note that last parameter, Readonly = False
With wbConn
.RefreshAll
'Debug.Assert False
AppExcel.AlertBeforeOverwriting = False 'This doesn't work
.Save 'BREAKS HERE - message boxes in Excel because the file is Read-only
.Close
End With
Set wbConn = Nothing
Set AppExcel = Nothing
End Sub
Try adding IgnoreReadOnlyRecommended:=True
尝试添加
IgnoreReadOnlyRecommended:=True
Set wbConn = AppExcel.Workbooks.Open("YourFilePath", True, False, IgnoreReadOnlyRecommended:=True)
If it does not work try directly:如果它不起作用直接尝试:
Set wbConn = AppExcel.Workbooks.Open("YourFilePath", True, IgnoreReadOnlyRecommended:=True)
Another additional solution would be to always .SaveAs
the file instead of using .Save
for which you could change the name of your file or use ConflictResolution
to overwrite the existing file另一个额外的解决方案是始终
.SaveAs
文件而不是使用.Save
,您可以更改文件名或使用ConflictResolution
覆盖现有文件
.SaveAs Filename:="YourFilePath", ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
I suggest you add AppExcel.DisplayAlerts = False
to the beginning of your code if you want to avoid the prompt messages that overwriting the file could cause如果您想避免覆盖文件可能导致的提示消息,我建议您将
AppExcel.DisplayAlerts = False
添加到代码的开头
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.