I guess I'm not seeing the difference in the code you just posted late yesterday and the code I originally posted in the first reply here nearly two years ago. He already confirmed that this wasn't really what he was looking for. It sounds like what is needed here, if working with an Excel Workbook type document is to reference the Excel namespace, then create an Excel.Workbook type variable, and set its value using the file path specified. Then use its property called Workbook.ReadOnly to check its status. That is a different status/property than the file explorer's file attribute called ReadOnly.
@ChristianAndersenIsmyname
Something like this maybe:
(The two custom functions are ones I commonly use for convenience, and can be skipped in simple situations.)
AddReference "Microsoft.Office.Interop.Excel.dll"
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Sub Main
Dim oExcel As Excel.Application = GetExcel
Dim oFileName As String = "K:\03. Native Drawings\01. Drawing Nos\Test1.xls"
Dim oWB As Excel.Workbook = GetWorkbook(oExcel, False, oFileName)
If IsNothing(oWB) Then
MsgBox("Could not get the specified Excel Workbook.", , "")
Exit Sub
End If
If oWB.ReadOnly Then
MsgBox("This Excel Workbook is currently ReadOnly.", , "")
Else
MsgBox("This Excel Workbook is Not ReadOnly.", , "")
End If
End Sub
Function GetExcel() As Excel.Application
Dim oXL As Excel.Application
Try
'try to find an already running instance of the Excel Application
oXL = GetObject(, "Excel.Application")
Catch
'it wasn't found open, so create an instance of it (start the application)
oXL = CreateObject("Excel.Application")
'oXL = New Microsoft.Office.Interop.Excel.Application
Catch
MsgBox("Failed to Get/Create an instance of the Excel Application. Exiting.", , "")
Exit Function
End Try
Return oXL
End Function
Function GetWorkbook(oExcelApp As Excel.Application, oNew As Boolean, Optional oTemplateOrFullFileName As String = vbNullString) As Workbook
If oExcelApp Is Nothing Then Return Nothing
Dim oFileProvided As Boolean = False
If oTemplateOrFullFileName <> "" Then oFileProvided = True
Dim oFileExists As Boolean = False
If oFileProvided Then oFileExists = System.IO.File.Exists(oTemplateOrFullFileName)
If oNew = False And oFileExists = False Then Return Nothing
Dim oWBs As Workbooks = oExcelApp.Workbooks
Dim oWB As Workbook = Nothing
If oNew = True AndAlso oFileExists Then
oWB = oWBs.Add(oTemplateOrFullFileName)
Return oWB
ElseIf oNew = True AndAlso oFileExists = False Then
oWB = oWBs.Add
Return oWB
End If
If oWBs.Count > 0 Then
For Each oWkbk As Workbook In oWBs
If oWkbk.FullName = oTemplateOrFullFileName Then Return oWkbk
Next
ElseIf oWBs.Count = 0 Then
oWB = oWBs.Open(oTemplateOrFullFileName)
Return oWB
End If
End Function
Wesley Crihfield

(Not an Autodesk Employee)