打开Excel文件,并检查它是否只读

我需要检查是否只有在读取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 

这里抛出一个exception:

 If GetAttr(UnapprovedFileName) And vbReadOnly Then 

“File”East.xlsx“找不到”。 但是,该文件打开,所以确实存在。 我想我需要参考文件没有path,但显然我在这里一点点。 有什么build议?

解:

 If StatVar.xlApp.ActiveWorkbook.ReadOnly = True Then