在尝试写入之前,我需要检查 Excel 文件是否为只读。这是我的代码:
Private Sub ButtonTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTest.Click
Dim UnapprovedAcctFile As String = "W:\TOM\ERIC\Award_Letters\Accounting Files\Unapproved\" & StatVar.xlApp.Sheets("New Calculator Input").Range("D30").Value & ".xlsx"
Dim UnapprovedFileName = StatVar.xlApp.Sheets("New Calculator Input").Range("D30").Value & ".xlsx"
If Not IO.File.Exists(UnapprovedAcctFile) Then
MessageBox.Show("Could not locate the accounting file for this school. Please contact an administrator for assistance.")
Exit Sub
Else
StatVar.xlWBUnapprovedAcctFile = StatVar.xlApp.Workbooks.Open(UnapprovedAcctFile)
If GetAttr(UnapprovedFileName) And vbReadOnly Then
MessageBox.Show("File is read only.")
End If
End If
End Sub
它在这里抛出异常:
If GetAttr(UnapprovedFileName) And vbReadOnly Then
例外是“找不到文件“East.xlsx”。但是,该文件打开,因此它确实存在。我以为我需要在没有路径的情况下引用文件,但显然我在这里有点偏离。有什么建议吗?