FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Open Excel file with TOleAuto() (Solved)
Posts: 581
Joined: Tue Oct 11, 2005 11:28 AM
Open Excel file with TOleAuto() (Solved)
Posted: Fri May 22, 2009 07:22 PM
Hi All,

I have a little doubt here... how to open an Excel file protected by a password? I tried this:

Code (fw): Select all Collapse
oExcel := TOleAuto():New( "Excel.Application" )
if Ole2TxtError() # "S_OK"
   MsgInfo("EXCEL não está instalado nesta máquina."+CRLF+"Impossível gerar planilha","ATENÇÃO")
   return nil
endif
oExcel:Workbooks:Open( cDirXLS+cArquivoX, , , ,"1111" )


But it gives me an error. Where am I wrong?

TIA,
Kleyber Derick



FWH / xHb / xDevStudio / SQLLIB
Posts: 581
Joined: Tue Oct 11, 2005 11:28 AM
Re: Open Excel file with TOleAuto()
Posted: Sat May 23, 2009 12:00 AM

The sintaxis for the command is:

Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword, ;
IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify, Converter, AddToMRU)

So, I don't understand what is happening.

Any clue?

TIA,

Kleyber Derick



FWH / xHb / xDevStudio / SQLLIB
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Open Excel file with TOleAuto()
Posted: Sat May 23, 2009 01:33 AM

Kleyber

I am not sure what type of error you are getting and why .. when I use OLE I always trap the command like this :

try
oExcel := CREATEOBJECT( "Excel.Application" )
catch oErr
Msginfo("error in opening Excel")
end try

I can not remember the code to interogate the error object oErr .. perhaps someone else can chime in here.

Rick Lipkin

Posts: 581
Joined: Tue Oct 11, 2005 11:28 AM
Re: Open Excel file with TOleAuto()
Posted: Sat May 23, 2009 01:43 AM
Hi Rick,

Thanks for answering... the error I get is this (from my error.log):

Code (fw): Select all Collapse
Error Excel.Application:WORKBOOKS/14  DISP_E_BADPARAMCOUNT: OPEN
   Argumentos   :
     [   1] = C   C:\Kleyber_TK\Money\Credfort\9649_4B6A716F_5782382000419_BH_19052009_CONSIGNADO.xls
     [   2] = C   
     [   3] = C   
     [   4] = C   
     [   5] = C   1111


Regards,
Kleyber Derick



FWH / xHb / xDevStudio / SQLLIB
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Open Excel file with TOleAuto()
Posted: Sat May 23, 2009 01:49 AM

Kleyber

Found this link with the same error :

http://www.rhinocerus.net/forum/lang-xh ... -file.html

However, it does look like you are opening the file with the full path .. just a quick observation .. can you shorten the file name and see if that helps ?

Rick

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Open Excel file with TOleAuto()
Posted: Sat May 23, 2009 02:27 AM
This code works.
Code (fw): Select all Collapse
   oBook    := oExcel:WorkBooks:Open( cFile, ;
                                      OleDefaultArg(), ;
                                      OleDefaultArg(), ;
                                      OleDefaultArg(), ;
                                      OleDefaultArg(), ;
                                      '1111' )
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Open Excel file with TOleAuto()
Posted: Sat May 23, 2009 03:37 AM
I suggest to have a general function to open excel workbooks ( you may keep it in your libaray )
Code (fw): Select all Collapse
function xlsBookOpen( ... )   // ( oExcel, cFile, < .. open params > )

   local aParams  := HB_AParams()
   local n
   local oExcel   := aParams[ 1 ]
   local oBook

   ADel( aParams, 1, .t. )

   for n := 2 to Len( aParams )
      if aParams[ n ] == nil
         aParams[ n ] := OleDefaultArg()
      endif
   next

   oBook  := HB_ExecFromArray( oExcel:WorkBooks, 'Open', aParams )

return oBook

Usage:
Code (fw): Select all Collapse
   oBook := xlsBookOpen( oExcel, cFile, , , , , '1111' )

If you are using a Class like TExcel, I suggest the following method
Code (fw): Select all Collapse
method Open( ... ) class TExcel  // ( cFile, < .. open params > )

   local aParams  := HB_AParams()
   local n
   local oBook

   for n := 2 to Len( aParams )
      if aParams[ n ] == nil
         aParams[ n ] := OleDefaultArg()
      endif
   next

   oBook  := HB_ExecFromArray( ::oExcel:WorkBooks, 'Open', aParams )

return oBook

Note: OleDefaultArg() is a function from Win32Ole.Prg of XHarbour
Regards



G. N. Rao.

Hyderabad, India
Posts: 581
Joined: Tue Oct 11, 2005 11:28 AM
Re: Open Excel file with TOleAuto()
Posted: Sat May 23, 2009 01:34 PM

Thanks for your good answer Nagesh. As I have xHarbour 1.1.0 and FWH 8.02 it does not have the function OleDefaultArg(). Is there a similar one in my xHarbour version?

Regards,

Kleyber Derick



FWH / xHb / xDevStudio / SQLLIB
Posts: 581
Joined: Tue Oct 11, 2005 11:28 AM
Re: Open Excel file with TOleAuto()
Posted: Sat May 23, 2009 01:36 PM
Rick Lipkin wrote:Kleyber

Found this link with the same error :

http://www.rhinocerus.net/forum/lang-xh ... -file.html

However, it does look like you are opening the file with the full path .. just a quick observation .. can you shorten the file name and see if that helps ?

Rick


Thanks Rick, but the error still persists.

Regards,
Kleyber Derick



FWH / xHb / xDevStudio / SQLLIB
Posts: 581
Joined: Tue Oct 11, 2005 11:28 AM
Re: Open Excel file with TOleAuto() (Solved)
Posted: Sat May 23, 2009 02:30 PM

Well I solved the problem. I got the Win32Ole.prg for a newer version of xHarbour and put in my project. Now it works like a charm.

Thank you Nagesh and Rick.

Best Regards,

Kleyber Derick



FWH / xHb / xDevStudio / SQLLIB
Posts: 5
Joined: Thu Jul 02, 2009 06:46 AM
Re: Open Excel file with TOleAuto() (Solved)
Posted: Thu Jul 02, 2009 07:02 AM
At work with excel files advise to use-xlsx corrupt,because tool helped me many times,has free status as far as I know,moreover program has many admissibilties such as can repair .xlsx even without touching your keyboard, just left click your mouse several times and all of your data will be recovered,automatically and attempts to repair xlsx corrupt, that was considered lost,recover maximum percents of your document manually, than the entire Excel workbook.

Continue the discussion