我正在使用vba尝试打开工作簿(如果它尚未打开)。VBA打开工作簿错误?
我遇到的问题是有时候工作簿可能会被另一个用户打开,所以如果工作簿被锁定,那么我想向用户提供一个选项来以只读方式打开工作簿。
代码:
'Open Planner
On Error Resume Next
Set WB = Workbooks("2017 Planner.xlsx")
On Error GoTo 0
If WB Is Nothing Then 'open workbook if not open
On Error GoTo Message4
Set WB = Workbooks.Open("G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\8. 2017\2017 Planner.xlsx", Password:="samples", WriteResPassword:="samples", UpdateLinks:=False)
Message4:
Dim answer2 As Integer
answer2 = MsgBox("Oooops!" & vbNewLine & vbNewLine & "We had trouble opening the planner with Read/Write access. We can open the file in Read-Only but this means the planner won't automatically be updated. Would you like to continue?", vbYesNo + vbQuestion, "Notice")
If answer2 = vbNo Then
Exit Sub
Else
Set WB = Workbooks.Open("G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\8. 2017\2017 Planner.xlsx", ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
End If
End If
出于某种原因,我在这条线得到一个错误1004:
Set WB = Workbooks.Open("G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\8. 2017\2017 Planner.xlsx", ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
您是否尝试过我码? – BOB
您在第一次打开电话时提供了'Password:=“samples”',但不在第二次打开。 –