robust_excel_ole 0.6 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. data/Changelog +12 -0
  2. data/README.rdoc +5 -3
  3. data/README_detail.rdoc +1 -1
  4. data/examples/edit_sheets/example_adding_sheets.rb +1 -1
  5. data/examples/edit_sheets/example_expanding.rb +1 -1
  6. data/examples/open_save_close/example_control_to_excel.rb +2 -2
  7. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +1 -1
  8. data/examples/open_save_close/example_if_obstructed_forget.rb +1 -1
  9. data/examples/open_save_close/example_if_unsaved_accept.rb +2 -2
  10. data/examples/open_save_close/example_read_only.rb +1 -1
  11. data/examples/open_save_close/example_simple.rb +1 -1
  12. data/lib/robust_excel_ole/book.rb +89 -111
  13. data/lib/robust_excel_ole/excel.rb +49 -50
  14. data/lib/robust_excel_ole/general.rb +3 -3
  15. data/lib/robust_excel_ole/reo_common.rb +78 -1
  16. data/lib/robust_excel_ole/sheet.rb +16 -23
  17. data/lib/robust_excel_ole/version.rb +1 -1
  18. data/spec/book_spec.rb +7 -7
  19. data/spec/book_specs/book_close_spec.rb +24 -5
  20. data/spec/book_specs/book_misc_spec.rb +16 -7
  21. data/spec/book_specs/book_open_spec.rb +15 -20
  22. data/spec/book_specs/book_save_spec.rb +21 -21
  23. data/spec/book_specs/book_sheet_spec.rb +3 -3
  24. data/spec/book_specs/book_unobtr_spec.rb +1 -1
  25. data/spec/data/book_with_blank.xls +0 -0
  26. data/spec/data/different_workbook.xls +0 -0
  27. data/spec/data/refed_wb.xls +0 -0
  28. data/spec/data/reference_workbook.xls +0 -0
  29. data/spec/data/referencing_wb.xls +0 -0
  30. data/spec/data/workbook.xls +0 -0
  31. data/spec/excel_spec.rb +87 -125
  32. data/spec/general_spec.rb +2 -2
  33. data/spec/reo_common_spec.rb +1 -1
  34. data/spec/sheet_spec.rb +13 -17
  35. metadata +7 -3
@@ -141,8 +141,7 @@ module RobustExcelOle
141
141
  else
142
142
  file = file_or_workbook
143
143
  ensure_excel(options)
144
- ensure_workbook(file, options)
145
- @ole_workbook.CheckCompatibility = options[:check_compatibility]
144
+ ensure_workbook(file, options)
146
145
  @can_be_closed = false if @can_be_closed.nil?
147
146
  end
148
147
  bookstore.store(self)
@@ -172,15 +171,32 @@ module RobustExcelOle
172
171
  begin
173
172
  object.excel
174
173
  rescue
175
- raise ExcelErrorOpen, "given object is neither an Excel, a Workbook, nor a Win32ole"
174
+ raise TypeErrorREO, "given object is neither an Excel, a Workbook, nor a Win32ole"
176
175
  end
177
176
  end
178
- #rescue
179
- # trace "no Excel, Book, or WIN32OLE object representing a Workbook or an Excel instance"
180
177
  end
181
178
 
182
179
  public
183
180
 
181
+ def self.open_in_current_excel(file, opts = { })
182
+ options = DEFAULT_OPEN_OPTS.merge(opts)
183
+ filename = General::absolute_path(file)
184
+ ole_workbook = WIN32OLE.connect(filename)
185
+ workbook = Book.new(ole_workbook)
186
+ workbook.visible = options[:visible] unless options[:visible].nil?
187
+ update_links_opt =
188
+ case options[:update_links]
189
+ when :alert; RobustExcelOle::XlUpdateLinksUserSetting
190
+ when :never; RobustExcelOle::XlUpdateLinksNever
191
+ when :always; RobustExcelOle::XlUpdateLinksAlways
192
+ else RobustExcelOle::XlUpdateLinksNever
193
+ end
194
+ workbook.UpdateLinks = update_links_opt
195
+ workbook.CheckCompatibility = options[:check_compatibility]
196
+ workbook
197
+ end
198
+
199
+
184
200
  def ensure_excel(options) # :nodoc: #
185
201
  return if @excel && @excel.alive?
186
202
  options[:excel] = options[:force_excel] ? options[:force_excel] : options[:default_excel]
@@ -191,23 +207,23 @@ module RobustExcelOle
191
207
 
192
208
  def ensure_workbook(file, options) # :nodoc: #
193
209
  file = @stored_filename ? @stored_filename : file
194
- raise(ExcelErrorOpen, "filename is nil") if file.nil?
210
+ raise(FileNameNotGiven, "filename is nil") if file.nil?
195
211
  unless File.exist?(file)
196
212
  if options[:if_absent] == :create
197
213
  @ole_workbook = excel_class.current.generate_workbook(file)
198
214
  else
199
- raise ExcelErrorOpen, "file #{file.inspect} not found"
215
+ raise FileNotFound, "file #{file.inspect} not found"
200
216
  end
201
217
  end
202
218
  @ole_workbook = @excel.Workbooks.Item(File.basename(file)) rescue nil
203
219
  if @ole_workbook then
204
220
  obstructed_by_other_book = (File.basename(file) == File.basename(@ole_workbook.Fullname)) &&
205
221
  (not (General::absolute_path(file) == @ole_workbook.Fullname))
206
- # if book is obstructed by a book with same name and different path
222
+ # if workbook is obstructed by a workbook with same name and different path
207
223
  if obstructed_by_other_book then
208
224
  case options[:if_obstructed]
209
225
  when :raise
210
- raise ExcelErrorOpen, "blocked by a book with the same name in a different path: #{@ole_workbook.Fullname.tr('\\','/')}"
226
+ raise WorkbookBlocked, "blocked by a workbook with the same name in a different path: #{@ole_workbook.Fullname.tr('\\','/')}"
211
227
  when :forget
212
228
  @ole_workbook.Close
213
229
  @ole_workbook = nil
@@ -219,7 +235,7 @@ module RobustExcelOle
219
235
  open_or_create_workbook(file, options)
220
236
  when :close_if_saved
221
237
  if (not @ole_workbook.Saved) then
222
- raise ExcelErrorOpen, "workbook with the same name in a different path is unsaved: #{@ole_workbook.Fullname.tr('\\','/')}"
238
+ raise WorkbookBlocked, "workbook with the same name in a different path is unsaved: #{@ole_workbook.Fullname.tr('\\','/')}"
223
239
  else
224
240
  @ole_workbook.Close
225
241
  @ole_workbook = nil
@@ -231,14 +247,14 @@ module RobustExcelOle
231
247
  @excel = excel_class.new(excel_options)
232
248
  open_or_create_workbook(file, options)
233
249
  else
234
- raise ExcelErrorOpen, ":if_obstructed: invalid option: #{options[:if_obstructed].inspect}"
250
+ raise OptionInvalid, ":if_obstructed: invalid option: #{options[:if_obstructed].inspect}"
235
251
  end
236
252
  else
237
253
  # book open, not obstructed by an other book, but not saved and writable
238
254
  if (not @ole_workbook.Saved) then
239
255
  case options[:if_unsaved]
240
256
  when :raise
241
- raise ExcelErrorOpen, "workbook is already open but not saved: #{File.basename(file).inspect}"
257
+ raise WorkbookNotSaved, "workbook is already open but not saved: #{File.basename(file).inspect}"
242
258
  when :forget
243
259
  @ole_workbook.Close
244
260
  @ole_workbook = nil
@@ -246,16 +262,14 @@ module RobustExcelOle
246
262
  when :accept
247
263
  # do nothing
248
264
  when :alert, :excel
249
- @excel.with_displayalerts true do
250
- open_or_create_workbook(file,options)
251
- end
265
+ @excel.with_displayalerts(true) { open_or_create_workbook(file,options) }
252
266
  when :new_excel
253
267
  excel_options = {:visible => false}.merge(options)
254
268
  excel_options[:reuse] = false
255
269
  @excel = excel_class.new(excel_options)
256
270
  open_or_create_workbook(file, options)
257
271
  else
258
- raise ExcelErrorOpen, ":if_unsaved: invalid option: #{options[:if_unsaved].inspect}"
272
+ raise OptionInvalid, ":if_unsaved: invalid option: #{options[:if_unsaved].inspect}"
259
273
  end
260
274
  end
261
275
  end
@@ -276,19 +290,21 @@ module RobustExcelOle
276
290
  rescue RuntimeError => msg
277
291
  trace "RuntimeError: #{msg.message}"
278
292
  if msg.message =~ /method missing: Excel not alive/
279
- raise ExcelErrorOpen, "Excel instance not alive or damaged"
293
+ raise ExcelDamaged, "Excel instance not alive or damaged"
280
294
  else
281
- raise ExcelErrorOpen, "unknown RuntimeError"
295
+ raise UnexpectedError, "unknown RuntimeError"
282
296
  end
283
297
  rescue WeakRef::RefError => msg
284
- trace "WeakRefError: #{msg.message}"
285
- raise ExcelErrorOpen, "#{msg.message}"
298
+ raise ExcelWeakRef, "#{msg.message}"
286
299
  end
287
300
  # workaround for linked workbooks for Excel 2007:
288
301
  # opening and closing a dummy workbook if Excel has no workbooks.
289
302
  # delay: with visible: 0.2 sec, without visible almost none
290
303
  count = workbooks.Count
291
- workbooks.Add if @excel.Version == "12.0" && count == 0
304
+ if @excel.Version == "12.0" && count == 0
305
+ workbooks.Add
306
+ #@excel.set_calculation(:automatic)
307
+ end
292
308
  update_links_opt =
293
309
  case options[:update_links]
294
310
  when :alert; RobustExcelOle::XlUpdateLinksUserSetting
@@ -296,27 +312,29 @@ module RobustExcelOle
296
312
  when :always; RobustExcelOle::XlUpdateLinksAlways
297
313
  else RobustExcelOle::XlUpdateLinksNever
298
314
  end
315
+ # probably bug in Excel: setting UpadateLinks seems to be dependent on calculation mode:
316
+ # update happens, if calcultion mode is automatic, does not, if calculation mode is manual
317
+ # parameter 'UpdateLinks' has no effect
299
318
  @excel.with_displayalerts(update_links_opt == :alert ? true : @excel.displayalerts) do
300
- # ??? determining update_links_opt here does not work, it is always XlUpdateLinksNever afterwords
301
319
  workbooks.Open(filename, { 'ReadOnly' => options[:read_only] , 'UpdateLinks' => update_links_opt } )
302
320
  end
303
- workbooks.Open(filename, { 'ReadOnly' => options[:read_only] } )
304
321
  workbooks.Item(1).Close if @excel.Version == "12.0" && count == 0
305
322
  rescue WIN32OLERuntimeError => msg
306
- trace "WIN32OLERuntimeError: #{msg.message}"
323
+ # trace "WIN32OLERuntimeError: #{msg.message}"
307
324
  if msg.message =~ /800A03EC/
308
- raise ExcelErrorOpen, "open: user canceled or open error"
325
+ raise WorkbookError, "open: user canceled or open error"
309
326
  else
310
- raise ExcelErrorOpen, "unknown WIN32OLERuntimeError"
327
+ raise UnexpectedError, "unknown WIN32OLERuntimeError"
311
328
  end
312
329
  end
313
330
  begin
314
331
  # workaround for bug in Excel 2010: workbook.Open does not always return the workbook with given file name
315
332
  @ole_workbook = workbooks.Item(File.basename(filename))
316
333
  self.visible = options[:visible] unless options[:visible].nil?
317
- @ole_workbook.UpdateLinks = update_links_opt
334
+ #@ole_workbook.UpdateLinks = update_links_opt
335
+ @ole_workbook.CheckCompatibility = options[:check_compatibility]
318
336
  rescue WIN32OLERuntimeError
319
- raise ExcelErrorOpen, "cannot find the file #{File.basename(filename).inspect}"
337
+ raise FileNotFound, "cannot find the file #{File.basename(filename).inspect}"
320
338
  end
321
339
  end
322
340
  end
@@ -333,32 +351,30 @@ module RobustExcelOle
333
351
  # :forget -> closes the workbook
334
352
  # :keep_open -> keep the workbook open
335
353
  # :alert or :excel -> gives control to excel
336
- # @raise ExcelErrorClose if the option :if_unsaved is :raise and the workbook is unsaved, or option is invalid
337
- # @raise ExcelErrorCanceled if the user has canceled
354
+ # @raise WorkbookNotSaved if the option :if_unsaved is :raise and the workbook is unsaved
355
+ # @raise OptionInvalid if the options is invalid
338
356
  def close(opts = {:if_unsaved => :raise})
339
357
  if (alive? && (not @ole_workbook.Saved) && writable) then
340
358
  case opts[:if_unsaved]
341
359
  when :raise
342
- raise ExcelErrorClose, "workbook is unsaved: #{File.basename(self.stored_filename).inspect}"
360
+ raise WorkbookNotSaved, "workbook is unsaved: #{File.basename(self.stored_filename).inspect}"
343
361
  when :save
344
362
  save
345
363
  close_workbook
346
364
  when :forget
347
- close_workbook
365
+ @excel.with_displayalerts(false) { close_workbook }
348
366
  when :keep_open
349
367
  # nothing
350
368
  when :alert, :excel
351
- @excel.with_displayalerts true do
352
- close_workbook
353
- end
369
+ @excel.with_displayalerts(true) { close_workbook }
354
370
  else
355
- raise ExcelErrorClose, ":if_unsaved: invalid option: #{opts[:if_unsaved].inspect}"
371
+ raise OptionInvalid, ":if_unsaved: invalid option: #{opts[:if_unsaved].inspect}"
356
372
  end
357
373
  else
358
374
  close_workbook
359
375
  end
360
- raise ExcelUserCanceled, "close: canceled by user" if alive? &&
361
- (opts[:if_unsaved] == :alert || opts[:if_unsaved] == :excel) && (not @ole_workbook.Saved)
376
+ trace "close: canceled by user" if alive? &&
377
+ (opts[:if_unsaved] == :alert || opts[:if_unsaved] == :excel) && (not @ole_workbook.Saved)
362
378
  end
363
379
 
364
380
  private
@@ -474,18 +490,17 @@ module RobustExcelOle
474
490
  end
475
491
 
476
492
  # simple save of a workbook.
477
- # @raise ExcelErrorSave if workbook is not alive or opened for read-only, or another error occurs
478
493
  # @return [Boolean] true, if successfully saved, nil otherwise
479
494
  def save
480
- raise ExcelErrorSave, "workbook is not alive" if (not alive?)
481
- raise ExcelErrorSave, "Not opened for writing (opened with :read_only option)" if @ole_workbook.ReadOnly
495
+ raise ObjectNotAlive, "workbook is not alive" if (not alive?)
496
+ raise WorkbookReadOnly, "Not opened for writing (opened with :read_only option)" if @ole_workbook.ReadOnly
482
497
  begin
483
498
  @ole_workbook.Save
484
499
  rescue WIN32OLERuntimeError => msg
485
500
  if msg.message =~ /SaveAs/ and msg.message =~ /Workbook/ then
486
- raise ExcelErrorSave, "workbook not saved"
501
+ raise WorkbookNotSaved, "workbook not saved"
487
502
  else
488
- raise ExcelErrorSaveUnknown, "unknown WIN32OELERuntimeError:\n#{msg.message}"
503
+ raise UnexpectedError, "unknown WIN32OELERuntimeError:\n#{msg.message}"
489
504
  end
490
505
  end
491
506
  true
@@ -507,14 +522,11 @@ module RobustExcelOle
507
522
  # :save -> saves the blocking workbook and closes it
508
523
  # :close_if_saved -> closes the blocking workbook, if it is saved,
509
524
  # otherwise raises an exception
510
- # @raise ExcelErrorSave if workbook is not alive, opened in read-only mode, invalid options,
511
- # the file already exists (with option :if_exists :raise),
512
- # the workbook is blocked by another one (with option :if_obstructed :raise)
513
525
  # @return [Book], the book itself, if successfully saved, raises an exception otherwise
514
526
  def save_as(file, opts = { } )
515
- raise(ExcelErrorSave, "filename is nil") if file.nil?
516
- raise ExcelErrorSave, "workbook is not alive" if (not alive?)
517
- raise ExcelErrorSave, "Not opened for writing (opened with :read_only option)" if @ole_workbook.ReadOnly
527
+ raise FileNameNotGiven, "filename is nil" if file.nil?
528
+ raise ObjectNotAlive, "workbook is not alive" if (not alive?)
529
+ raise WorkbookReadOnly, "Not opened for writing (opened with :read_only option)" if @ole_workbook.ReadOnly
518
530
  options = {
519
531
  :if_exists => :raise,
520
532
  :if_obstructed => :raise,
@@ -529,7 +541,7 @@ module RobustExcelOle
529
541
  begin
530
542
  File.delete(file)
531
543
  rescue Errno::EACCES
532
- raise ExcelErrorSave, "workbook is open and used in Excel"
544
+ raise WorkbookBeingUsed, "workbook is open and used in Excel"
533
545
  end
534
546
  end
535
547
  when :alert, :excel
@@ -538,9 +550,9 @@ module RobustExcelOle
538
550
  end
539
551
  return self
540
552
  when :raise
541
- raise ExcelErrorSave, "file already exists: #{File.basename(file).inspect}"
553
+ raise FileAlreadyExists, "file already exists: #{File.basename(file).inspect}"
542
554
  else
543
- raise ExcelErrorSave, ":if_exists: invalid option: #{options[:if_exists].inspect}"
555
+ raise OptionInvalid, ":if_exists: invalid option: #{options[:if_exists].inspect}"
544
556
  end
545
557
  end
546
558
  blocking_workbook =
@@ -552,15 +564,15 @@ module RobustExcelOle
552
564
  if blocking_workbook then
553
565
  case options[:if_obstructed]
554
566
  when :raise
555
- raise ExcelErrorSave, "blocked by another workbook: #{blocking_workbook.Fullname.tr('\\','/')}"
567
+ raise WorkbookBlocked, "blocked by another workbook: #{blocking_workbook.Fullname.tr('\\','/')}"
556
568
  when :forget
557
569
  # nothing
558
570
  when :save
559
571
  blocking_workbook.Save
560
572
  when :close_if_saved
561
- raise ExcelErrorSave, "blocking workbook is unsaved: #{File.basename(file).inspect}" unless blocking_workbook.Saved
573
+ raise WorkbookBlocked, "blocking workbook is unsaved: #{File.basename(file).inspect}" unless blocking_workbook.Saved
562
574
  else
563
- raise ExcelErrorSave, ":if_obstructed: invalid option: #{options[:if_obstructed].inspect}"
575
+ raise OptionInvalid, ":if_obstructed: invalid option: #{options[:if_obstructed].inspect}"
564
576
  end
565
577
  blocking_workbook.Close
566
578
  end
@@ -583,14 +595,10 @@ module RobustExcelOle
583
595
  bookstore.store(self)
584
596
  rescue WIN32OLERuntimeError => msg
585
597
  if msg.message =~ /SaveAs/ and msg.message =~ /Workbook/ then
586
- if options[:if_exists] == :alert || options[:if_exists] == :excel then
587
- raise ExcelErrorSave, "not saved or canceled by user"
588
- else
589
- return nil
590
- end
591
- # another possible semantics. raise ExcelErrorSaveFailed, "could not save Workbook"
598
+ trace "save: canceled by user" if options[:if_exists] == :alert || options[:if_exists] == :excel
599
+ # another possible semantics. raise WorkbookError, "could not save Workbook"
592
600
  else
593
- raise ExcelErrorSaveUnknown, "unknown WIN32OELERuntimeError:\n#{msg.message}"
601
+ raise UnexpectedError, "unknown WIN32OELERuntimeError:\n#{msg.message}"
594
602
  end
595
603
  end
596
604
  end
@@ -604,8 +612,8 @@ module RobustExcelOle
604
612
  begin
605
613
  sheet_class.new(@ole_workbook.Worksheets.Item(name))
606
614
  rescue WIN32OLERuntimeError => msg
607
- raise ExcelError, "could not return a sheet with name #{name.inspect}"
608
- trace "#{msg.message}"
615
+ raise NameNotFound, "could not return a sheet with name #{name.inspect}"
616
+ # trace "#{msg.message}"
609
617
  end
610
618
  end
611
619
 
@@ -622,7 +630,7 @@ module RobustExcelOle
622
630
  # @option opts [Symbol] :as new name of the copied sheet
623
631
  # @option opts [Symbol] :before a sheet before which the sheet shall be inserted
624
632
  # @option opts [Symbol] :after a sheet after which the sheet shall be inserted
625
- # @raise ExcelErrorSheet if the sheet name already exists
633
+ # @raise NameAlreadyExists if the sheet name already exists
626
634
  # @return [Sheet] the copied sheet
627
635
  def copy_sheet(sheet, opts = { })
628
636
  new_sheet_name = opts.delete(:as)
@@ -632,7 +640,7 @@ module RobustExcelOle
632
640
  begin
633
641
  new_sheet.name = new_sheet_name if new_sheet_name
634
642
  rescue WIN32OLERuntimeError => msg
635
- msg.message =~ /800A03EC/ ? raise(ExcelErrorSheet, "sheet name already exists") : raise(ExcelErrorSheetUnknown)
643
+ msg.message =~ /800A03EC/ ? raise(NameAlreadyExists, "sheet name already exists") : raise(UnexpectedError)
636
644
  end
637
645
  new_sheet
638
646
  end
@@ -643,7 +651,7 @@ module RobustExcelOle
643
651
  # @option opts [Symbol] :as new name of the copied added sheet
644
652
  # @option opts [Symbol] :before a sheet before which the sheet shall be inserted
645
653
  # @option opts [Symbol] :after a sheet after which the sheet shall be inserted
646
- # @raise ExcelErrorSheet if the sheet name already exists
654
+ # @raise NameAlreadyExists if the sheet name already exists
647
655
  # @return [Sheet] the added sheet
648
656
  def add_empty_sheet(opts = { })
649
657
  new_sheet_name = opts.delete(:as)
@@ -653,7 +661,7 @@ module RobustExcelOle
653
661
  begin
654
662
  new_sheet.name = new_sheet_name if new_sheet_name
655
663
  rescue WIN32OLERuntimeError => msg
656
- msg.message =~ /800A03EC/ ? raise(ExcelErrorSheet, "sheet name already exists") : raise(ExcelErrorSheetUnknown)
664
+ msg.message =~ /800A03EC/ ? raise(NameAlreadyExists, "sheet name already exists") : raise(UnexpectedError)
657
665
  end
658
666
  new_sheet
659
667
  end
@@ -665,7 +673,6 @@ module RobustExcelOle
665
673
  # @option opts [Symbol] :as new name of the copied or added sheet
666
674
  # @option opts [Symbol] :before a sheet before which the sheet shall be inserted
667
675
  # @option opts [Symbol] :after a sheet after which the sheet shall be inserted
668
- # @raise ExcelErrorSheet if the sheet name already exists
669
676
  # @return [Sheet] the copied or added sheet
670
677
  def add_or_copy_sheet(sheet = nil, opts = { })
671
678
  if sheet.is_a? Hash
@@ -710,16 +717,14 @@ module RobustExcelOle
710
717
  # @param [String] name the name of the range
711
718
  # @param [Hash] opts the options
712
719
  # @option opts [Symbol] :default the default value that is provided if no contents could be returned
713
- # @raise ExcelError if range name is not in the workbook or if range value could not be evaluated
714
720
  # @return [Variant] the contents of a range with given name
715
-
716
721
  def nameval(name, opts = {:default => nil})
717
722
  default_val = opts[:default]
718
723
  begin
719
724
  name_obj = self.Names.Item(name)
720
725
  rescue WIN32OLERuntimeError
721
726
  return default_val if default_val
722
- raise ExcelError, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
727
+ raise NameNotFound, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
723
728
  end
724
729
  begin
725
730
  value = name_obj.RefersToRange.Value
@@ -728,12 +733,12 @@ module RobustExcelOle
728
733
  value = self.sheet(1).Evaluate(name_obj.Name)
729
734
  rescue WIN32OLERuntimeError
730
735
  return default_val if default_val
731
- raise ExcelError, "cannot evaluate name #{name.inspect} in #{File.basename(self.stored_filename).inspect}"
736
+ raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{File.basename(self.stored_filename).inspect}"
732
737
  end
733
738
  end
734
739
  if value == RobustExcelOle::XlErrName # -2146826259
735
740
  return default_val if default_val
736
- raise ExcelError, "cannot evaluate name #{name.inspect} in #{File.basename(self.stored_filename).inspect}"
741
+ raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{File.basename(self.stored_filename).inspect}"
737
742
  end
738
743
  return default_val if default_val && value.nil?
739
744
  value
@@ -742,46 +747,44 @@ module RobustExcelOle
742
747
  # sets the contents of a range
743
748
  # @param [String] name the name of a range
744
749
  # @param [Variant] value the contents of the range
745
- # @raise ExcelError if range name is not in the workbook or if value could not be assigned to range
746
750
  def set_nameval(name, value)
747
751
  begin
748
752
  name_obj = self.Names.Item(name)
749
753
  rescue WIN32OLERuntimeError
750
- raise ExcelError, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
754
+ raise NameNotFound, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
751
755
  end
752
756
  begin
753
757
  name_obj.RefersToRange.Value = value
754
758
  rescue WIN32OLERuntimeError
755
- raise ExcelError, "cannot assign value to range named #{name.inspect} in #{File.basename(self.stored_filename).inspect}"
759
+ raise RangeNotEvaluatable, "cannot assign value to range named #{name.inspect} in #{File.basename(self.stored_filename).inspect}"
756
760
  end
757
761
  end
758
762
 
759
763
  # renames a range
760
764
  # @param [String] name the previous range name
761
765
  # @param [String] new_name the new range name
762
- # @raise ExcelError if name is not in the file, or if new_name cannot be set
763
766
  def rename_range(name, new_name)
764
767
  begin
765
768
  item = self.Names.Item(name)
766
769
  rescue WIN32OLERuntimeError
767
- raise ExcelError, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
770
+ raise NameNotFound, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
768
771
  end
769
772
  begin
770
773
  item.Name = new_name
771
774
  rescue WIN32OLERuntimeError
772
- raise ExcelError, "name error in #{File.basename(self.stored_filename).inspect}"
775
+ raise UnexpectedError, "name error in #{File.basename(self.stored_filename).inspect}"
773
776
  end
774
777
  end
775
778
 
776
779
  # brings workbook to foreground, makes it available for heyboard inputs, makes the Excel instance visible
777
- # @raise ExcelError if workbook cannot be activated
778
780
  def activate
779
781
  @excel.visible = true
780
782
  begin
781
- Win32API.new("user32","SetForegroundWindow","I","I").call(@excel.hwnd) # Excel 2010
783
+ Win32API.new("user32","SetForegroundWindow","I","I").call # Excel 2010
784
+ SetForegroundWindow.call(@excel.Hwnd)
782
785
  @ole_workbook.Activate # Excel 2007
783
- rescue WIN32OLERuntimeError
784
- raise ExcelError, "cannot activate"
786
+ rescue WIN32OLERuntimeError => msg
787
+ raise UnexpectedError, "cannot activate: #{message.msg}"
785
788
  end
786
789
  end
787
790
 
@@ -796,7 +799,7 @@ module RobustExcelOle
796
799
  saved = @ole_workbook.Saved
797
800
  @excel.visible = true if visible_value
798
801
  @ole_workbook.Windows(@ole_workbook.Name).Visible = visible_value if @excel.Visible
799
- save if saved && (not self.ReadOnly)
802
+ @ole_workbook.Saved = saved
800
803
  end
801
804
 
802
805
  # returns true, if the workbook reacts to methods, false otherwise
@@ -893,7 +896,7 @@ module RobustExcelOle
893
896
  def method_missing(name, *args) # :nodoc: #
894
897
  if name.to_s[0,1] =~ /[A-Z]/
895
898
  begin
896
- raise ExcelError, "method missing: workbook not alive" unless alive?
899
+ raise ObjectNotAlive, "method missing: workbook not alive" unless alive?
897
900
  @ole_workbook.send(name, *args)
898
901
  rescue WIN32OLERuntimeError => msg
899
902
  if msg.message =~ /unknown property or method/
@@ -912,31 +915,6 @@ public
912
915
 
913
916
  Workbook = Book
914
917
 
915
- class ExcelError < RuntimeError # :nodoc: #
916
- end
917
918
 
918
- class ExcelErrorOpen < ExcelError # :nodoc: #
919
- end
920
-
921
- class ExcelErrorClose < ExcelError # :nodoc: #
922
- end
923
-
924
- class ExcelErrorSave < ExcelError # :nodoc: #
925
- end
926
-
927
- class ExcelErrorSaveFailed < ExcelErrorSave # :nodoc: #
928
- end
929
-
930
- class ExcelErrorSaveUnknown < ExcelErrorSave # :nodoc: #
931
- end
932
-
933
- class ExcelUserCanceled < RuntimeError # :nodoc: #
934
- end
935
-
936
- class ExcelErrorSheet < ExcelError # :nodoc: #
937
- end
938
-
939
- class ExcelErrorSheetUnknown < ExcelErrorSheet # :nodoc: #
940
- end
941
919
 
942
920
  end