Open Excel file with TOleAuto() (Solved)

Post Reply
User avatar
Kleyber
Posts: 581
Joined: Tue Oct 11, 2005 11:28 am
Location: São Luiz, Brasil

Open Excel file with TOleAuto() (Solved)

Post by Kleyber »

Hi All,

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

Code: Select all

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,
Last edited by Kleyber on Sun May 24, 2009 1:28 am, edited 1 time in total.
Kleyber Derick

FWH / xHb / xDevStudio / SQLLIB
User avatar
Kleyber
Posts: 581
Joined: Tue Oct 11, 2005 11:28 am
Location: São Luiz, Brasil

Re: Open Excel file with TOleAuto()

Post by Kleyber »

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
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Open Excel file with TOleAuto()

Post by Rick Lipkin »

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
User avatar
Kleyber
Posts: 581
Joined: Tue Oct 11, 2005 11:28 am
Location: São Luiz, Brasil

Re: Open Excel file with TOleAuto()

Post by Kleyber »

Hi Rick,

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

Code: Select all

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
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Open Excel file with TOleAuto()

Post by Rick Lipkin »

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
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Open Excel file with TOleAuto()

Post by nageswaragunupudi »

This code works.

Code: Select all

   oBook    := oExcel:WorkBooks:Open( cFile, ;
                                      OleDefaultArg(), ;
                                      OleDefaultArg(), ;
                                      OleDefaultArg(), ;
                                      OleDefaultArg(), ;
                                      '1111' )

 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Open Excel file with TOleAuto()

Post by nageswaragunupudi »

I suggest to have a general function to open excel workbooks ( you may keep it in your libaray )

Code: Select all

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: Select all

   oBook := xlsBookOpen( oExcel, cFile, , , , , '1111' )
 
If you are using a Class like TExcel, I suggest the following method

Code: Select all

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
User avatar
Kleyber
Posts: 581
Joined: Tue Oct 11, 2005 11:28 am
Location: São Luiz, Brasil

Re: Open Excel file with TOleAuto()

Post by Kleyber »

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
User avatar
Kleyber
Posts: 581
Joined: Tue Oct 11, 2005 11:28 am
Location: São Luiz, Brasil

Re: Open Excel file with TOleAuto()

Post by Kleyber »

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
User avatar
Kleyber
Posts: 581
Joined: Tue Oct 11, 2005 11:28 am
Location: São Luiz, Brasil

Re: Open Excel file with TOleAuto() (Solved)

Post by Kleyber »

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
zlatan24
Posts: 5
Joined: Thu Jul 02, 2009 6:46 am

Re: Open Excel file with TOleAuto() (Solved)

Post by zlatan24 »

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.
Post Reply