Message 1 of 3
Checking if excel doc is read-only or open

Not applicable
09-04-2017
05:21 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
How would I adapt this code to do the following in the specified scenarios:
- Document is open and not read-only - Write to open workbook, save and close workbook.
-
Document is open and is read-only - Inform user that workbook is read-only and exit rule.
-
Document is closed and not read-only - Open workbook and write to it, save and close workbook.
- 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