I think your problem is with the scope of the variable @my_workbook because it's a member of a class now. I recommend finding a good Ruby resource that describes the various variable types - I'm sure any resource you find would do a better job than I could.
Good luck! -Tiffany On Feb 25, 1:16 am, [email protected] wrote: > Hi, > > Tried this out: > > I have one class called Utilities.rb with the following content: > """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" > require "watir" > require 'win32ole' > > class Utilities > > def connect(path_file) > excel = WIN32OLE::new('excel.application') > @my_workbook = excel.Workbooks.open(path_file) > end > > def disconnect(workbook) > workbook.Close > excel.Quit > end > end > """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" > > This is "Main" with this content: > > """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" > require 'watir' > require'Utilities' > > util = Utilities.new() > util.connect("C:\\Programfiler\\Ruby\\FPSdata.xls") > > #read and write to @my_workbook > #worksheet = ut...@my_workbook.worksheet(1) > > util.disconnect(@my_workbook) > """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" > > As you might have guessed, I want to separate my Excel connect/ > disconnect into a separate class and call this class' methods in my > main class. > > I get the following error message: > > Undefines methos Close for nil:NilClass (NoMethodError) > > Cheers > > On 24 Feb, 23:39, Tiffany Fodor <[email protected]> wrote: > > > Try this: > > > def connect(path_file) > > excel = WIN32OLE::new('excel.application') > > �...@my_workbook = excel.Workbooks.open(path_file) > > end > > > def disconnect(path_file, workbook) > > workbook.SaveAs(path_file) > > workbook.close > > end > > > Then you can call your methods like this: > > > connect("C:\\Spreadsheets\\MySpreadsheet.xls") > > > #read and write to @my_workbook > > > disconnect("C:\\Spreadsheets\\MyNewSpreadsheet.xls", @my_workbook) > > > If you're hoping to have more than one spreadsheet open at a time, > > you'll run into trouble. In that case, it may be easier to just open > > each spreadsheet and close it without using the methods since each > > method is only a couple lines of code anyway. > > > I hope this helps! > > > -Tiffany > > > On Feb 24, 12:03 pm, [email protected] wrote: > > > > Hi, > > > So I can write> > def connect(workbook) > > > > > worksheet = workbook.WorkSheets(1) > > > > > #Manipulate the workbook cells as in i.e. get the cell values here > > > > > end > > > > And have another method ´disconnect´ > > > > as this: > > > > > > def disconnect(workbook) > > > > > workbook.close() > > > > > end > > > > This will refer to the same object? > > > > Cheers > > > > On Feb 24, 3:59 pm, Tiffany Fodor <[email protected]> wrote: > > > > > Sorry, I should have also included that you can pass 'workbook' to > > > > methods and write to the file within those methods. > > > > > On Feb 24, 1:02 am, [email protected] wrote: > > > > > > This might be a ruby question, bit is it possible to have an WIN32OLE > > > > > object asparameterto a method? > > > > > > excel = WIN32OLE::new("excel.Application") > > > > > workbook = excel.Workbooks.Open("C:\\Programfiler\\Ruby\ > > > > > \FPSdata.xls") > > > > > > def connect(workbook) > > > > > worksheet = workbook.WorkSheets(1) > > > > > puts "You are now connected to Excel" > > > > > end– Skjul sitert tekst – > > > – Vis sitert tekst – --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Watir General" group. To post to this group, send email to [email protected] Before posting, please read the following guidelines: http://wiki.openqa.org/display/WTR/Support To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/watir-general -~----------~----~----~----~------~----~------~--~---
