简体   繁体   English

停止 ms 访问 VBA 打开 Excel 文件只读

[英]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.

相关问题 使用 VBA 将 MS Access 记录集导出到 Excel 中的多个工作表/选项卡会导致只读文件 - Exporting MS Access recordsets to multiple worksheets/tabs in Excel results in Read-Only files Using VBA 从 Outlook 打开 Excel 时 VBA 只读 - VBA Read-only when Opening Excel from Outlook 从Excel VBA连接到只读Access数据库 - connecting to a read-only Access db from Excel VBA Excel VBA宏在勾选了“只读”属性时赋予了对文件的写访问权限 - Excel VBA macro to give Write access to file when Read-only property attribute ticked 使用文件时 Excel VBA 以只读方式打开 - Excel VBA Open Read-Only when File is in Use Excel VBA 另存为“只读” PDF 文件 - Excel VBA SaveAs "Read-only" PDF file Excel VBA将数据写入第二个工作簿,但开始打开只读版本,因为“_已经打开 - Excel VBA writes data to second workbook, but starts opening read-only versions because " _ is already open 如何在使用 VBA 打开 Excel 工作簿时绕过 SharePoint“只读”警报。 - How to bypass SharePoint "Read-Only" alert when opening an Excel Workbook with VBA. 使用VBA打开.xlsx,“文件使用中”错误。 只读不起作用 - Opening .xlsx with VBA, File in Use error. Read-only not working 无论如何,ADO 在保存之前从只读 excel 文件中读取更新的数据? (VBA) - Anyway for ADO to read updated data from a read-only excel file before save? (VBA)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM