繁体   English   中英

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

[英]Open Excel file and check if it's read only

我需要先检查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”。 但是,该文件将打开,因此它确实存在。 我以为我需要引用不带路径的文件,但是显然我在这里有点离题。 有什么建议吗?

解:

If StatVar.xlApp.ActiveWorkbook.ReadOnly = True Then

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM