Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

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.
(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)
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. 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.

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 RecordsSystem FilesConnection 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
question from:https://stackoverflow.com/questions/65924683/stop-ms-access-vba-opening-excel-file-read-only

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try adding 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 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...