Checking if excel doc is read-only or open

Checking if excel doc is read-only or open

Anonymous
Not applicable
1,868 Views
2 Replies
Message 1 of 3

Checking if excel doc is read-only or open

Anonymous
Not applicable

How would I adapt this code to do the following in the specified scenarios:

 

  1. Document is open and not read-only - Write to open workbook, save and close workbook.
  2. Document is open and is read-only - Inform user that workbook is read-only and exit rule.

  3.  

    Document is closed and not read-only - Open workbook and write to it, save and close workbook.

  4. Document is closed and is read-only - Inform user that the workbook is read-only and exit rule.

 

 

'define the file to create/open
myXLS_File = ThisDoc.Path & "\GAS SPRING CALCULATION.xlsm"
    
'define Excel Application object
excelApp = CreateObject("Excel.Application")
'set Excel to run visibly, change to false if you want to run it invisibly
excelApp.Visible = False
'suppress prompts (such as the compatibility checker)
excelApp.DisplayAlerts = False
    
'check for existing file 
If Dir(myXLS_File) <> "" Then
    'workbook exists, open it
    excelWorkbook = excelApp.Workbooks.Open(myXLS_File)
    dataSheet = ExcelWorkbook.Worksheets(1)
Else
    'workbook doesn't exist, notify user
    MessageBox.Show("The excel document " & ThisDoc.Path & "\GAS SPRING CALCULATION.xlsm" & " doesn't exist.", "Open Document Error",MessageBoxButtons.OK,MessageBoxIcon.Error)
    excelApp.Quit
    excelApp = Nothing
    Return
End If

'Write to some cells

'Save document, close the workbook and the Excel Application
excelWorkbook.SaveAs (myXLS_File)
excelWorkbook.Close
excelWorkbook = Nothing
excelApp.Quit
excelApp = Nothing

 

0 Likes
1,869 Views
2 Replies
Replies (2)
Message 2 of 3

MechMachineMan
Advisor
Advisor

Google-fu results:

 

"Excel Read-Only":

- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbook-readonly-property-excel

Notes: To check read only in excel, it's likely you have to open the file first. Which, "checking it while closed" would actually mean opening it, checking it, then closing the file.

 

 

"File Attribute Read-only"

- https://msdn.microsoft.com/en-us/library/system.io.fileinfo.isreadonly(v=vs.110).aspx


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 3

b_sharanraj
Advocate
Advocate

 

You can check this way

 

If excelWorkbook.readonly = True
MessageBox.Show("The excel document " & myXLS_File & " is Readonly")
Else
MessageBox.Show("The excel document " & myXLS_File & " is Accessible")
End If

 

Regards

B.Sharan Raj

0 Likes