我需要在尝试写入 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"。”但是,这个文件已经打开了,所以它确实存在。我原以为需要在没有路径的情况下引用该文件,但显然我有些偏差。有什么建议吗?