2013-07-22 119 views
1

我需要檢查在嘗試寫入Excel文件之前是否只讀取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 

它在這裏拋出一個異常:「文件‘East.xlsx’找不到」

If GetAttr(UnapprovedFileName) And vbReadOnly Then 

唯一的例外是但是,該文件打開,所以它確實存在。我認爲我需要參考沒有路徑的文件,但顯然我在這裏有點偏離。有什麼建議?

+0

這聽起來像您使用的是相對路徑。檢查當前目錄。 – SLaks

+0

當然有一個更簡單的解決方案,但我會玩這個想法一點 –

回答

1

解決方案:

If StatVar.xlApp.ActiveWorkbook.ReadOnly = True Then