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

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


听起来你正在使用相对路径。检查当前目录。 - undefined
肯定有更简单的解决方案,但我会先试试这个想法 - undefined
1个回答

3

解决方案:

If StatVar.xlApp.ActiveWorkbook.ReadOnly = True Then

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接