Class: RobustExcelOle::Excel

Inherits:
RangeOwners show all
Defined in:
lib/robust_excel_ole/excel.rb

Constant Summary collapse

@@hwnd2excel =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RangeOwners

#name_object, #nameval, #rangeval, #set_nameval, #set_rangeval

Methods inherited from REOCommon

#own_methods, puts_hash, tr1, trace

Constructor Details

#initialize(options = {}) ⇒ Excel

:nodoc: #



104
105
# File 'lib/robust_excel_ole/excel.rb', line 104

def initialize(options= {}) # :nodoc: #
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

:nodoc: #



642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/robust_excel_ole/excel.rb', line 642

def method_missing(name, *args)    # :nodoc: #
  if name.to_s[0,1] =~ /[A-Z]/ 
    begin          
      raise ObjectNotAlive, "method missing: Excel not alive" unless alive?
      @ole_excel.send(name, *args)
    rescue WIN32OLERuntimeError => msg
      if msg.message =~ /unknown property or method/
        raise VBAMethodMissingError, "unknown VBA property or method #{name.inspect}"
      else 
        raise msg
      end
    end
  else  
    super 
  end
end

Instance Attribute Details

#calculationObject

Returns the value of attribute calculation



22
23
24
# File 'lib/robust_excel_ole/excel.rb', line 22

def calculation
  @calculation
end

#createdObject

Returns the value of attribute created



16
17
18
# File 'lib/robust_excel_ole/excel.rb', line 16

def created
  @created
end

#displayalertsObject

Returns the value of attribute displayalerts



21
22
23
# File 'lib/robust_excel_ole/excel.rb', line 21

def displayalerts
  @displayalerts
end

#ole_excelObject

Returns the value of attribute ole_excel



15
16
17
# File 'lib/robust_excel_ole/excel.rb', line 15

def ole_excel
  @ole_excel
end

#screenupdatingObject

Returns the value of attribute screenupdating



23
24
25
# File 'lib/robust_excel_ole/excel.rb', line 23

def screenupdating
  @screenupdating
end

#visibleObject

setter methods are implemented below



20
21
22
# File 'lib/robust_excel_ole/excel.rb', line 20

def visible
  @visible
end

#workbookObject

Returns the value of attribute workbook



17
18
19
# File 'lib/robust_excel_ole/excel.rb', line 17

def workbook
  @workbook
end

Class Method Details

.book_classObject

:nodoc: #



625
626
627
628
629
630
631
632
# File 'lib/robust_excel_ole/excel.rb', line 625

def self.book_class   
  @book_class ||= begin
    module_name = self.parent_name
    "#{module_name}::Book".constantize
  rescue NameError => e
    book
  end
end

.close_all(options = {:if_unsaved => :raise}, &blk) ⇒ Fixnum

closes all Excel instances remark: the returned number of closed Excel instances is valid only for known Excel instances if there are unknown Excel instances (opened not via this class), then they are counted as 1 options:

:if_unsaved    if unsaved workbooks are open in an Excel instance
                    :raise (default) -> raises an exception       
                    :save            -> saves the workbooks before closing
                    :forget          -> closes the excel instance without saving the workbooks 
                    :alert           -> give control to Excel

Parameters:

  • options (Hash) (defaults to: {:if_unsaved => :raise})

    the options

Options Hash (options):

  • :if_unsaved (Symbol)

    :raise, :save, :forget, or :alert

  • block (Proc)

Returns:

  • (Fixnum, Fixnum)

    number of closed Excel instances, number of errors



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/robust_excel_ole/excel.rb', line 239

def self.close_all(options = {:if_unsaved => :raise}, &blk)
  options[:if_unsaved] = blk if blk
  finished_number = error_number = overall_number = 0
  first_error = nil
  finishing_action = proc do |excel|
    if excel
      begin
        overall_number += 1
        finished_number += excel.close(:if_unsaved => options[:if_unsaved])
      rescue
        first_error = $!
        #trace "error when finishing #{$!}"
        error_number += 1
      end
    end
  end

  # known Excel-instances
  @@hwnd2excel.each do |hwnd, wr_excel|
    if wr_excel.weakref_alive?
      excel = wr_excel.__getobj__
      if excel.alive?
        excel.displayalerts = false
        finishing_action.call(excel)
      end
    else
      @@hwnd2excel.delete(hwnd) 
    end
  end

  # unknown Excel-instances
  old_error_number = error_number
  9.times do |index|
    sleep 0.1
    excel = new(WIN32OLE.connect('Excel.Application')) rescue nil
    finishing_action.call(excel) if excel
    free_all_ole_objects unless error_number > 0 and options[:if_unsaved] == :raise 
    break if not excel
    break if error_number > old_error_number # + 3        
  end

  raise first_error if (options[:if_unsaved] == :raise and first_error) or first_error.class == OptionInvalid

  [finished_number, error_number]
end

.contains_unsaved_workbooks?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/robust_excel_ole/excel.rb', line 171

def self.contains_unsaved_workbooks?
  not Excel.current.unsaved_workbooks.empty?
end

.create(options = {}) ⇒ Excel

creates a new Excel instance

Parameters:

  • options (Hash) (defaults to: {})

    the options

Options Hash (options):

  • :displayalerts (Variant)
  • :visible (Boolean)
  • :calculation (Symbol)
  • :screenupdating (Boolean)

Returns:

  • (Excel)

    a new Excel instance



36
37
38
# File 'lib/robust_excel_ole/excel.rb', line 36

def self.create(options = {})
  new(options.merge({:reuse => false}))
end

.current(options = {}) ⇒ Excel

connects to the current (first opened) Excel instance, if such a running Excel instance exists

returns a new Excel instance, otherwise

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :displayalerts (Variant)
  • :visible (Boolean)
  • :calculation (Symbol)
  • :screenupdating (Boolean)

Returns:

  • (Excel)

    an Excel instance



47
48
49
# File 'lib/robust_excel_ole/excel.rb', line 47

def self.current(options = {})
  new(options.merge({:reuse => true}))
end

.excels_numberObject



379
380
381
382
# File 'lib/robust_excel_ole/excel.rb', line 379

def self.excels_number
  processes = WIN32OLE.connect("winmgmts:\\\\.").InstancesOf("win32_process")
  processes.select{ |p| p.name == "EXCEL.EXE"}.size
end

.free_all_ole_objectsObject

frees all OLE objects in the object space



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/robust_excel_ole/excel.rb', line 336

def self.free_all_ole_objects     
  anz_objekte = 0
  ObjectSpace.each_object(WIN32OLE) do |o|        
    anz_objekte += 1
    #trace "#{anz_objekte} name: #{(o.Name rescue (o.Count rescue "no_name"))} ole_object_name: #{(o.ole_object_name rescue nil)} type: #{o.ole_type rescue nil}"
    #trace [:Name, (o.Name rescue (o.Count rescue "no_name"))]
    #trace [:ole_object_name, (o.ole_object_name rescue nil)]
    #trace [:methods, (o.ole_methods rescue nil)] unless (o.Name rescue false)
    #trace o.ole_type rescue nil
    begin
      o.ole_free
      #trace "olefree OK"
    rescue
      #trace "olefree_error: #{$!}"
      #trace $!.backtrace.first(9).join "\n"
    end
  end
  #trace "went through #{anz_objekte} OLE objects"
end

.hwnd2excel(hwnd) ⇒ Object

:nodoc: #



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/robust_excel_ole/excel.rb', line 419

def self.hwnd2excel(hwnd)   # :nodoc: #
  excel_weakref = @@hwnd2excel[hwnd]
  if excel_weakref
    if excel_weakref.weakref_alive?
      excel_weakref.__getobj__
    else
      trace "dead reference to an Excel"
      begin 
        @@hwnd2excel.delete(hwnd)
        nil
      rescue
        trace "Warning: deleting dead reference failed! (hwnd: #{hwnd.inspect})"
      end
    end
  end
end

.initObject



357
358
359
# File 'lib/robust_excel_ole/excel.rb', line 357

def self.init
  @@hwnd2excel = {}
end

.kill_allFixnum

kill all Excel instances

Returns:

  • (Fixnum)

    number of killed Excel processes



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/robust_excel_ole/excel.rb', line 363

def self.kill_all
  number = 0
  WIN32OLE.connect("winmgmts:\\\\.").InstancesOf("win32_process").each do |p|
    begin
      if p.name == "EXCEL.EXE"  
        Process.kill('KILL', p.processid)       
        number += 1
      end
    rescue 
       #trace "kill error: #{$!}"
    end
  end
  init
  number
end

.known_excel_instancesObject

returns all Excel objects for all Excel instances opened with RobustExcelOle,



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/robust_excel_ole/excel.rb', line 385

def self.known_excel_instances
  pid2excel = {}
  @@hwnd2excel.each do |hwnd,wr_excel|
    if wr_excel.weakref_alive?
      excel = wr_excel.__getobj__
      process_id = Win32API.new("user32", "GetWindowThreadProcessId", ["I","P"], "I")
      pid_puffer = " " * 32
      process_id.call(hwnd, pid_puffer)
      pid = pid_puffer.unpack("L")[0]
      pid2excel[pid] = excel
    end
  end
  processes = WIN32OLE.connect("winmgmts:\\\\.").InstancesOf("win32_process")
  #excel_processes = processes.select{ |p| p.name == "EXCEL.EXE" && pid2excel.include?(p.processid)}
  #excel_processes.map{ |p| Excel.new(pid2excel[p.processid]) }
  processes.select{ |p| Excel.new(pid2excel[p.processid]) if p.name == "EXCEL.EXE" && pid2excel.include?(p.processid)}
  result = []
  processes.each do |p|
    if p.name == "EXCEL.EXE"
      if pid2excel.include?(p.processid)
        excel = pid2excel[p.processid]
        result << excel
      end
      # how to connect to an (interactively opened) Excel instance and get a WIN32OLE object?
      # after that, lift it to an Excel object
    end
  end
  result
end

.new(win32ole_excel = nil, options = { }) ⇒ Excel

returns an Excel instance

options:

:reuse            connects to an already running Excel instance (true) or
                  creates a new Excel instance (false)  (default: true)
:visible          makes the Excel visible               (default: false)
:displayalerts    enables or disables DisplayAlerts     (true, false, :if_visible (default))   
:calculation      calculation mode is being forced to be manual (:manual) or automatic (:automtic)
                  or is not being forced (default: nil)
:screenupdating  turns on or off screen updating (default: true)

Parameters:

  • (optional) (Win32Ole)

    a WIN32OLE object representing an Excel instance

  • options (Hash) (defaults to: { })

    the options

Options Hash (options):

  • :reuse (Boolean)
  • :visible (Boolean)
  • :displayalerts (Variant)
  • :screenupdating (Boolean)
  • :calculation (Symbol)

Returns:

  • (Excel)

    an Excel instance



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/robust_excel_ole/excel.rb', line 68

def self.new(win32ole_excel = nil, options = { })
  if win32ole_excel.is_a? Hash
    options = win32ole_excel
    win32ole_excel = nil
  end
  ole_xl = win32ole_excel unless win32ole_excel.nil?
  options = {:reuse => true}.merge(options)
  ole_xl = current_excel if options[:reuse] == true
  ole_xl ||= WIN32OLE.new('Excel.Application')
  hwnd = ole_xl.HWnd
  stored = hwnd2excel(hwnd)
  if stored and stored.alive?
    result = stored
  else 
    result = super(options)
    result.instance_variable_set(:@ole_excel, ole_xl)        
    WIN32OLE.const_load(ole_xl, RobustExcelOle) unless RobustExcelOle.const_defined?(:CONSTANTS)
    @@hwnd2excel[hwnd] = WeakRef.new(result)
  end
  
  unless options.is_a? WIN32OLE
    begin
      reused = options[:reuse] && stored && stored.alive?
      unless reused
        options = {:displayalerts => :if_visible, :visible => false, :screenupdating => true}.merge(options)
      end
      result.visible = options[:visible] unless options[:visible].nil?
      result.displayalerts = options[:displayalerts] unless options[:displayalerts].nil?
      result.calculation = options[:calculation] unless options[:calculation].nil?
      result.screenupdating = options[:screenupdating] unless options[:screenupdating].nil?
      result.created = !reused
    end
  end    
  result
end

:nodoc: #



440
441
442
443
444
445
446
# File 'lib/robust_excel_ole/excel.rb', line 440

def self.print_hwnd2excel    
  @@hwnd2excel.each do |hwnd,wr_excel|
    excel_string = (wr_excel.weakref_alive? ? wr_excel.__getobj__.to_s : "weakref not alive") 
    printf("hwnd: %8i => excel: %s\n", hwnd, excel_string)
  end
  @@hwnd2excel.size
end

.unsaved_known_workbooksObject

returns unsaved workbooks in known (not opened by user) Excel instances



464
465
466
467
468
469
470
471
# File 'lib/robust_excel_ole/excel.rb', line 464

def self.unsaved_known_workbooks  
  result = []
  @@hwnd2excel.each do |hwnd,wr_excel| 
    excel = wr_excel.__getobj__ if wr_excel.weakref_alive?
    result << excel.unsaved_workbooks
  end
  result
end

Instance Method Details

#==(other_excel) ⇒ Object

returns true, if the Excel instances are alive and identical, false otherwise



449
450
451
# File 'lib/robust_excel_ole/excel.rb', line 449

def == other_excel
  self.Hwnd == other_excel.Hwnd  if other_excel.is_a?(Excel) && self.alive? && other_excel.alive?
end

#[](name) ⇒ Object

returns the value of a range

Parameters:

  • name (String)

    the name of a range



606
607
608
# File 'lib/robust_excel_ole/excel.rb', line 606

def [] name
  nameval(name)
end

#[]=(name, value) ⇒ Object

sets the value of a range

Parameters:

  • name (String)

    the name of the range

  • value (Variant)

    the contents of the range



613
614
615
# File 'lib/robust_excel_ole/excel.rb', line 613

def []= (name, value)
  set_nameval(name,value, :color => 42) # 42 - aqua-marin, 7-green
end

#alive?Boolean

returns true, if the Excel instances responds to VBA methods, false otherwise

Returns:

  • (Boolean)


454
455
456
457
458
459
460
# File 'lib/robust_excel_ole/excel.rb', line 454

def alive?
  @ole_excel.Name
  true
rescue
  #trace $!.message
  false
end

#book_classObject

:nodoc: #



634
635
636
# File 'lib/robust_excel_ole/excel.rb', line 634

def book_class        
  self.class.book_class
end

#Calculation=(calculation_vba_mode) ⇒ Object

VBA method overwritten



547
548
549
550
551
552
553
554
555
# File 'lib/robust_excel_ole/excel.rb', line 547

def Calculation= calculation_vba_mode
  case calculation_vba_mode
  when XlCalculationManual
    @calculation = :manual
  when XlCalculationAutomatic
    @calculation = :automatic
  end
  @ole_excel.Calculation = calculation_vba_mode
end

#close(options = {:if_unsaved => :raise}) ⇒ Object

closes the Excel

Parameters:

  • options (Hash) (defaults to: {:if_unsaved => :raise})

    the options

Options Hash (options):

  • :if_unsaved (Symbol)

    :raise, :save, :forget, :alert

  • :hard (Boolean)

    :if_unsaved if unsaved workbooks are open in an Excel instance

    :raise (default) -> raises an exception       
    :save            -> saves the workbooks before closing
    :forget          -> closes the Excel instance without saving the workbooks 
    :alert           -> Excel takes over
    


294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/robust_excel_ole/excel.rb', line 294

def close(options = {:if_unsaved => :raise})
  finishing_living_excel = self.alive?
  if finishing_living_excel then
    hwnd = (@ole_excel.HWnd rescue nil)
    close_workbooks(:if_unsaved => options[:if_unsaved])
    @ole_excel.Quit
    if false and defined?(weak_wkbks)  and weak_wkbks.weakref_alive? then
      weak_wkbks.ole_free
    end
    weak_xl = WeakRef.new(@ole_excel)
  else
    weak_xl = nil
  end 
  @ole_excel = nil
  GC.start
  sleep 0.1
  if finishing_living_excel then
    if hwnd then          
      process_id = Win32API.new("user32", "GetWindowThreadProcessId", ["I","P"], "I")
      pid_puffer = " " * 32
      process_id.call(hwnd, pid_puffer)
      pid = pid_puffer.unpack("L")[0]
      begin
        Process.kill("KILL", pid) 
      rescue 
        #trace "kill_error: #{$!}"
      end
    end
    @@hwnd2excel.delete(hwnd)
    if weak_xl.weakref_alive? then
       #if WIN32OLE.ole_reference_count(weak_xlapp) > 0
      begin
        weak_xl.ole_free
      rescue
        #trace "weakref_probl_olefree"
      end
    end
  end
  weak_xl ? 1 : 0
end

#close_workbooks(options = {:if_unsaved => :raise}) ⇒ Object

closes workbooks

Parameters:

  • options (Hash) (defaults to: {:if_unsaved => :raise})

    a customizable set of options

Options Hash (options):

  • :if_unsaved (Symbol)

    :raise, :save, :forget, :alert, Proc :if_unsaved if unsaved workbooks are open in an Excel instance

    :raise (default) -> raises an exception       
    :save            -> saves the workbooks before closing
    :forget          -> closes the Excel instance without saving the workbooks 
    :alert           -> let Excel do it
    


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/robust_excel_ole/excel.rb', line 193

def close_workbooks(options = {:if_unsaved => :raise})
 return if not self.alive?
 weak_wkbks = @ole_excel.Workbooks
 if not unsaved_workbooks.empty? then
   case options[:if_unsaved]
   when Proc
     options[:if_unsaved].call(self, unsaved_workbooks)
   when :raise
     raise UnsavedWorkbooks, "Excel contains unsaved workbooks"
   when :alert
     #nothing
   when :forget
     unsaved_workbooks.each {|m| m.Saved = true}
   when :save
     unsaved_workbooks.each {|m| m.Save}
   else
     raise OptionInvalid, ":if_unsaved: invalid option: #{options[:if_unsaved].inspect}"
   end
 end
 begin
   @ole_excel.Workbooks.Close
 rescue WIN32OLERuntimeError => msg
   if msg.message =~ /800A03EC/
     raise ExcelREOError, "user canceled or runtime error"
   else 
     raise UnexpectedREOError, "unknown WIN32OLERuntimeError: #{msg.message}"
   end
 end   
 weak_wkbks = nil
 weak_wkbks = @ole_excel.Workbooks
 weak_wkbks = nil
end

#excelObject

:nodoc: #



415
416
417
# File 'lib/robust_excel_ole/excel.rb', line 415

def excel   
  self
end

#focusObject



594
595
596
597
598
599
600
601
# File 'lib/robust_excel_ole/excel.rb', line 594

def focus
  self.visible = true
  #if not Windows10 then 
  Win32API.new("user32","SetForegroundWindow","I","I").call(@ole_excel.Hwnd)
  #else
  #Win32API.new("user32","SetForegroundWindow","","I").call
  #end
end

#for_all_workbooks(options) ⇒ Object

set options in all workbooks



579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/robust_excel_ole/excel.rb', line 579

def for_all_workbooks(options)
  ole_workbooks = begin
    @ole_excel.Workbooks
  rescue WIN32OLERuntimeError => msg
    if msg.message =~ /failed to get Dispatch Interface/
      raise ExcelDamaged, "Excel instance not alive or damaged"
    else
      raise ExcelREOError, "workbooks could not be determined"
    end 
  end
  ole_workbooks.each do |ole_workbook|
    book_class.open(ole_workbook).for_this_workbook(options)
  end
end

#for_this_instance(options) ⇒ Object

set options in this Excel instance



570
571
572
# File 'lib/robust_excel_ole/excel.rb', line 570

def for_this_instance(options)
  self.class.new(@ole_excel, options)
end

#generate_workbook(file_name) ⇒ Object

generates, saves, and closes empty workbook

Raises:



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/robust_excel_ole/excel.rb', line 478

def generate_workbook file_name    
  raise FileNameNotGiven, "filename is nil" if file_name.nil?                
  self.Workbooks.Add                           
  empty_workbook = self.Workbooks.Item(self.Workbooks.Count)          
  filename = General::absolute_path(file_name).gsub("/","\\")
  unless File.exists?(filename)
    begin
      empty_workbook.SaveAs(filename) 
    rescue WIN32OLERuntimeError => msg
      #if msg.message =~ /SaveAs/ and msg.message =~ /Workbook/ then
        raise FileNotFound, "could not save workbook with filename #{file_name.inspect}"
      #else
      #  # todo some time: find out when this occurs : 
      #  raise UnexpectedREOError, "unknown WIN32OLERuntimeError with filename #{file_name.inspect}: \n#{msg.message}"
      #end
    end      
  end
  empty_workbook                               
end

#hwndObject

:nodoc: #



436
437
438
# File 'lib/robust_excel_ole/excel.rb', line 436

def hwnd   
  self.Hwnd rescue nil
end

#inspectObject

:nodoc: #



621
622
623
# File 'lib/robust_excel_ole/excel.rb', line 621

def inspect           
  self.to_s
end

:nodoc: #



473
474
475
# File 'lib/robust_excel_ole/excel.rb', line 473

def print_workbooks   
  self.Workbooks.each {|w| trace "#{w.Name} #{w}"}
end

#recreate(opts = {}) ⇒ Excel

reopens a closed Excel instance options: reopen_workbooks (default: false): reopen the workbooks in the Excel instances :visible (default: false), :displayalerts (default: :if_visible), :calculation (default: false)

Parameters:

  • opts (Hash) (defaults to: {})

    the options

Options Hash (opts):

  • :reopen_workbooks (Boolean)
  • :displayalerts (Boolean)
  • :visible (Boolean)
  • :calculation (Boolean)

Returns:

  • (Excel)

    an Excel instance



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/robust_excel_ole/excel.rb', line 116

def recreate(opts = {})      
  unless self.alive?
    opts = {
      :visible => @visible ? @visible : false,
      :displayalerts => @displayalerts ? @displayalerts : :if_visible          
    }.merge(opts)
    @ole_excel = WIN32OLE.new('Excel.Application')
    self.visible = opts[:visible]
    self.displayalerts = opts[:displayalerts]        
    self.calculation = opts[:calculation]
    if opts[:reopen_workbooks]
      books = book_class.books
      books.each do |book|
        book.reopen if ((not book.alive?) && book.excel.alive? && book.excel == self)
      end        
    end
  end
  self 
end

#retain_saved_workbooksObject

retain the saved status of all workbooks



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/robust_excel_ole/excel.rb', line 157

def retain_saved_workbooks
  saved = []
  @ole_excel.Workbooks.each {|w| saved << w.Saved}
  begin
    yield self
  ensure
    i = 0
    @ole_excel.Workbooks.each do |w|
      w.Saved = saved[i] if saved[i]
      i = i + 1 
    end
  end
end

#set_options(options) ⇒ Object



574
575
576
# File 'lib/robust_excel_ole/excel.rb', line 574

def set_options(options)
  for_this_instance(options)
end

#to_sObject

:nodoc: #



617
618
619
# File 'lib/robust_excel_ole/excel.rb', line 617

def to_s              
  "#<Excel: " + "#{hwnd}" + ("#{"not alive" unless self.alive?}") + ">"
end

#unsaved_workbooksObject

returns unsaved workbooks (win32ole objects)



176
177
178
179
180
181
182
183
184
# File 'lib/robust_excel_ole/excel.rb', line 176

def unsaved_workbooks
  unsaved_workbooks = []   
  begin   
    @ole_excel.Workbooks.each {|w| unsaved_workbooks << w unless (w.Saved || w.ReadOnly)}
  rescue RuntimeError => msg
    raise ExcelDamaged, "Excel instance not alive or damaged" if msg.message =~ /failed to get Dispatch Interface/
  end
  unsaved_workbooks
end

#with_calculation(calculation_mode) ⇒ Object

sets calculation mode in a block



558
559
560
561
562
563
564
565
566
567
# File 'lib/robust_excel_ole/excel.rb', line 558

def with_calculation(calculation_mode)
  return unless calculation_mode
  old_calculation_mode = @ole_excel.Calculation
  begin
    self.calculation = calculation_mode
    yield self
  ensure
    @ole_excel.Calculation = old_calculation_mode if @ole_excel.Calculation.is_a?(Fixnum)
  end
end

#with_displayalerts(displayalerts_value) ⇒ Object

sets DisplayAlerts in a block



499
500
501
502
503
504
505
506
507
# File 'lib/robust_excel_ole/excel.rb', line 499

def with_displayalerts displayalerts_value
  old_displayalerts = self.displayalerts
  self.displayalerts = displayalerts_value
  begin
     yield self
  ensure
    self.displayalerts = old_displayalerts if alive?
  end
end