Failed to save Excel file after change with Ruby & win32ole

Using http://ruby-doc.org/stdlib/libdoc/win32ole/rdoc/classes/WIN32OLE.html as a guide, I wrote the following:

require 'win32ole' excel = WIN32OLE.new('Excel.Application') excel.visible = false #Setting this is 'true' doesn't reveal anything workbook = excel.workbooks.open('C:\myspreadsheet.xlsx') worksheet = workbook.worksheets('sheet1') worksheet.Activate data = worksheet.UsedRange.Value p data.size #This works! - My spreadsheet has 3987 rows. p data[3932] #This works, too! - I can "see" row 3932. worksheet.Rows(3932).Insert #Insert a row above row 3932 data = worksheet.UsedRange.Value p data.size #Returns 3988! This would seem to indicate that I've successfully added a row since it was just 3987. workbook.saved = true #Save the workbook and quit. excel.ActiveWorkbook.Close(0) excel.Quit() 

When I open the Excel spreadsheet after that, it does not change. Any ideas?

+4
source share
1 answer

Setting the Saved property of the Workbook object to True does not save the workbook. This property is used as a flag to indicate whether the workbook has unsaved changes. Setting it to True is an easy way to prevent the "Do you want to save ..." dialog box from appearing when Excel is closed.

To really save the workbook, you need the Save method of the Workbook object. This method does not return anything, so I would suggest that workbook.Save will do the trick (I have no Ruby experience, unfortunately, therefore I'm not 100% sure)

+3
source

All Articles