1. Juan Jose Marquez Martin
  2. PowerBuilder
  3. Wednesday, 30 August 2023 07:07 AM UTC

Hi, good day!

 

I'm using the OLE object connection to Excel for treat information and files. Hi have the next code implemented in one of my applications.

 

//                 More code before this 


li_connect_excel = lole_excel.ConnectToObject( ls_source_file )

IF li_connect_excel < RET_OK THEN
Parent.plb_filesToLoad.deleteItem( li_file )
Parent.plb_filesToLoad.insertItem( ls_source_file, IMA_FILE_FAIL, li_file )
ls_message = wfs_errorText( li_connect_excel )
Yield()
MessageBox( 'Export Error', 'Unable to open/load spreadsheet' + NEW_LINE + ls_source_file + NEW_LINE + ls_message + NEW_LINE + 'Please check if the file is open by another process.' )
wf_checkAcceptVisible()
This.pictureName = '..\Img\bt_apply.png'
This.enabled = True
w_loadFile.pb_prior.enabled = True
wf_setTabsStatus( True )
EXIT
END IF

li_works = lole_excel.Application.workBooks.Count

FOR li_work = 1 TO li_works
ls_workBook_name = Upper( lole_excel.Application.workBooks(li_work).Name )
IF ls_workBook_name = Upper( ls_fileName[ li_file ] ) THEN
EXIT
END IF
NEXT

IF li_work > li_works THEN
Parent.plb_filesToLoad.deleteItem( li_file )
Parent.plb_filesToLoad.insertItem( ls_source_file, IMA_FILE_FAIL, li_file )
Yield()
MessageBox( 'Export Error', 'Unable to find ' + NEW_LINE + ls_source_file + NEW_LINE + 'Please check if the file is open by another process.' )
wf_checkAcceptVisible()
This.pictureName = '..\Img\bt_apply.png'
This.enabled = True
w_loadFile.pb_prior.enabled = True
wf_setTabsStatus( True )
EXIT
END IF

ll_fileLength = FileLength( ls_target_file )

IF ll_fileLength <> FILE_ERROR THEN

IF NOT FileDelete( ls_target_file ) THEN
Parent.plb_filesToLoad.deleteItem( li_file )
Parent.plb_filesToLoad.insertItem( ls_source_file, IMA_FILE_FAIL, li_file )
Yield()
MessageBox( 'Export Error', 'Unable to delete file ' + NEW_LINE + ls_target_file + NEW_LINE + 'Please check if the file is open by another process.' )
wf_checkAcceptVisible()
This.pictureName = '..\Img\bt_apply.png'
This.enabled = True
w_loadFile.pb_prior.enabled = True
wf_setTabsStatus( True )
EXIT
END IF

END IF

li_sheet_number = li_sheet_numbers[ li_file ]
IF li_sheet_number = POS_NO_POSITION THEN
li_sheet_number = POS_FIRST
END IF

FOR li_column = 1 TO li_columns[ li_file ]
lole_excel.Application.workBooks(li_work).workSheets(li_sheet_number).Columns( li_column ).Replace( Char(10), '' )
NEXT

TRY
lole_excel.Application.workBooks(li_work).workSheets(li_sheet_number).SaveAs( ls_target_file, EXCEL_xlCSV )   // Line 193

// EXCEL_xlCSV = 6


CATCH ( runtimeerror er )
MessageBox( gnvo_app.is_appName + ' Runtime Error' &
, er.GetMessage() + NEW_LINE + NEW_LINE + 'Contact with Tech department' &
, StopSign! )
wf_checkAcceptVisible()
This.pictureName = '..\Img\bt_apply.png'
This.enabled = True
w_loadFile.pb_prior.enabled = True
wf_setTabsStatus( True )
lb_exit = True
Parent.plb_filesToLoad.deleteItem( li_file )
Parent.plb_filesToLoad.insertItem( ls_target_file, IMA_FILE_FAIL, li_file )

FINALLY
lole_excel.Application.DisplayAlerts = False
lole_excel.Application.workBooks(li_work).Close()

END TRY

//                 More code after this 

 

This works meanwhile some days ago, then suddenly in some machines the next error appears

 

 

All .dll Excel related are available, in fact I copied all .dll of the runtime in the same folder of the application in order to prevent an access issue, but no matter...

 

The Excel installed on the machines are the 365 version.

 

Any idea? Very appreciated if some light on the end of the tunnel appears ;)

 

Thanks a lot in advance for your contribution.

 

René Ullrich Accepted Answer Pending Moderation
  1. Wednesday, 30 August 2023 07:26 AM UTC
  2. PowerBuilder
  3. # 1

You should try to catch OLERuntimeError exception. It contains a property "description" that may have additional information about the error.

Comment
  1. Juan Jose Marquez Martin
  2. Wednesday, 30 August 2023 08:15 AM UTC
I'll investigate with this, I hope that I'll find the solution to my problem. Thanks a ton, René
  1. Helpful
  1. Juan Jose Marquez Martin
  2. Wednesday, 30 August 2023 09:55 AM UTC
Unfortunately, the OLERuntimeError .Description and .Source Properties are empty when the exception is raised.
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Wednesday, 30 August 2023 09:36 AM UTC
  2. PowerBuilder
  3. # 2

Hi,

Please read the first post on this Q&A to paste code. It's very hard to read your code like this and it really disencourages people to try and decifer it to give you any help: https://community.appeon.com/index.php/qna/q-a/q-a-forum-tip-including-source-code-in-your-question 

I think the problem is your for loop. ll_i (my example below) will always be one higher that maximum of the "to" of the for loop:

I you do this for example:

long ll_i

for ll_i = 1 to 10
	// do nothing
next

// ll_i will be 11, not 10 !!!
messagebox('debug', ll_i)

So in other words, after your loop, it looks like you might expect li_work to be equal to li_works, but it's not, it's higher: "li_works + 1":

FOR li_work = 1 TO li_works
    ls_workBook_name = Upper( lole_excel.Application.workBooks(li_work).Name )
    IF ls_workBook_name = Upper( ls_fileName[ li_file ] ) THEN
        EXIT
    END IF
NEXT

IF li_work > li_works THEN
    .....

li_work will always be greater than li_works, but won't point anything valid in Excel.

Comment
  1. Juan Jose Marquez Martin
  2. Wednesday, 30 August 2023 09:48 AM UTC
So sorry, Miguel, I only put the complete section of code for show that no data fault into the problematic line.



The loop it's crashed always by the statements -- IF ls_workBook_name = Upper( ls_fileName[ li_file ] ) THEN EXIT END IF-- when the book name it's found; then, only if the bookName isn't found the comparison the li_work variable it's grater then li_works



The application is working properly with this code until a week ago, seems as a Office (or another product) upgrade changes something.

  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Wednesday, 30 August 2023 10:35 AM UTC
  2. PowerBuilder
  3. # 3

Okay, I see what you about li_work.

First to make your code readable:

// More code before this 


li_connect_excel = lole_excel.ConnectToObject( ls_source_file )

IF li_connect_excel < RET_OK THEN
	Parent.plb_filesToLoad.deleteItem( li_file )
	Parent.plb_filesToLoad.insertItem( ls_source_file, IMA_FILE_FAIL, li_file )
	ls_message = wfs_errorText( li_connect_excel )
	Yield()
	MessageBox( 'Export Error', 'Unable to open/load spreadsheet' + NEW_LINE + ls_source_file + NEW_LINE + ls_message + NEW_LINE + 'Please check if the file is open by another process.' )
	wf_checkAcceptVisible()
	This.pictureName = '..\Img\bt_apply.png'
	This.enabled = True
	w_loadFile.pb_prior.enabled = True
	wf_setTabsStatus( True )
	EXIT
END IF

li_works = lole_excel.Application.workBooks.Count

FOR li_work = 1 TO li_works
	ls_workBook_name = Upper( lole_excel.Application.workBooks(li_work).Name )
	IF ls_workBook_name = Upper( ls_fileName[ li_file ] ) THEN
		EXIT
	END IF
NEXT

IF li_work > li_works THEN
	Parent.plb_filesToLoad.deleteItem( li_file )
	Parent.plb_filesToLoad.insertItem( ls_source_file, IMA_FILE_FAIL, li_file )
	Yield()
	MessageBox( 'Export Error', 'Unable to find ' + NEW_LINE + ls_source_file + NEW_LINE + 'Please check if the file is open by another process.' )
	wf_checkAcceptVisible()
	This.pictureName = '..\Img\bt_apply.png'
	This.enabled = True
	w_loadFile.pb_prior.enabled = True
	wf_setTabsStatus( True )
	EXIT
END IF

ll_fileLength = FileLength( ls_target_file )

IF ll_fileLength <> FILE_ERROR THEN

	IF NOT FileDelete( ls_target_file ) THEN
		Parent.plb_filesToLoad.deleteItem( li_file )
		Parent.plb_filesToLoad.insertItem( ls_source_file, IMA_FILE_FAIL, li_file )
		Yield()
		MessageBox( 'Export Error', 'Unable to delete file ' + NEW_LINE + ls_target_file + NEW_LINE + 'Please check if the file is open by another process.' )
		wf_checkAcceptVisible()
		This.pictureName = '..\Img\bt_apply.png'
		This.enabled = True
		w_loadFile.pb_prior.enabled = True
		wf_setTabsStatus( True )
		EXIT
	END IF

END IF

li_sheet_number = li_sheet_numbers[ li_file ]
IF li_sheet_number = POS_NO_POSITION THEN
	li_sheet_number = POS_FIRST
END IF

FOR li_column = 1 TO li_columns[ li_file ]
	lole_excel.Application.workBooks(li_work).workSheets(li_sheet_number).Columns( li_column ).Replace( Char(10), '' )
NEXT

TRY

	lole_excel.Application.workBooks(li_work).workSheets(li_sheet_number).SaveAs( ls_target_file, EXCEL_xlCSV )   // Line 193

// EXCEL_xlCSV = 6

CATCH ( runtimeerror er )
	MessageBox( gnvo_app.is_appName + ' Runtime Error' &
	, er.GetMessage() + NEW_LINE + NEW_LINE + 'Contact with Tech department' &
	, StopSign! )
	wf_checkAcceptVisible()
	This.pictureName = '..\Img\bt_apply.png'
	This.enabled = True
	w_loadFile.pb_prior.enabled = True
	wf_setTabsStatus( True )
	lb_exit = True
	Parent.plb_filesToLoad.deleteItem( li_file )
	Parent.plb_filesToLoad.insertItem( ls_target_file, IMA_FILE_FAIL, li_file )

FINALLY
	lole_excel.Application.DisplayAlerts = False
	lole_excel.Application.workBooks(li_work).Close()

END TRY

// More code after this 

You say it fails on "some" machines. Do they all have write access to the drive / folder where you're trying to do the SaveAs() to?

I find it very weird that Ren'e's suggestion of doing a Catch with OLERuntimeError doesn't give you more information.

What's to see in the windows Event Viewer?

regards

Comment
  1. Chris Pollach @Appeon
  2. Wednesday, 30 August 2023 12:05 PM UTC
Hi Miguel;

I wonder if Juan's problem is that some users are upgrading to Office 365 where its DOM (Document Object Model) has changed. This will then affect various OLE command syntax.

Food for thought.

Regards.... Chris
  1. Helpful
  1. Juan Jose Marquez Martin
  2. Wednesday, 30 August 2023 12:43 PM UTC
The problem isn't an upgrading, all users and machines where is executed the application have Office 365, the problem did occurs after the migration of the PB applications from PB 2017R3 Build 1880 to PB 2022 Build 1900
  1. Helpful
  1. Chris Pollach @Appeon
  2. Wednesday, 30 August 2023 16:49 PM UTC
Hi Juan;

Since it worked in development and when deployed but suddenly stopped working - I would suggest looking any any recent O/S and/or O365 updates or even fixes. Food for thought.

Regards ... Chris
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Wednesday, 30 August 2023 14:02 PM UTC
  2. PowerBuilder
  3. # 4

Hi again,

On the PC of a user with the problem:

What happens if you use excel itself to open one of the failing sheets and then try to do a SaveAs with CSV format?

Maybe there's some kind of prompt from excel.

Comment
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Wednesday, 30 August 2023 14:20 PM UTC
  2. PowerBuilder
  3. # 5

Here's some of my code. I seem to remember that "DisplayAlerts" has to be FALSE:

aole_xlApp.DisplayAlerts = FALSE

try
	if FileExists(as_path) then
		FileDelete(as_path)
	end if
	aole_xlApp.ActiveWorkBook.SaveAs(as_path, li_saveType)
catch (oleruntimeError oe)
	messagebox('OleError', oe.GetMessage())
	return
end try

aole_xlApp.DisplayAlerts = TRUE

 

Set your

lole_excel.Application.DisplayAlerts = FALSE

Try to do the SaveAs again and after the SaveAs, set it to TRUE again.

Comment
There are no comments made yet.
Juan Jose Marquez Martin Accepted Answer Pending Moderation
  1. Wednesday, 30 August 2023 14:35 PM UTC
  2. PowerBuilder
  3. # 6

Finally the mistery was resolved... I don't know if cut my veins or leave them long as the hair!! hahaha

 

The folder where I did try to save the file doesn't exists, only create it, and "le voila" the application works without problem.

 

Obviously, the machines, users, environments where the folder exists, no problem.

 

Miguel, René and Chris, thank you so much for your contributions for clarify the issue.

 

A true pleasure.

Comment
  1. Miguel Leeuwe
  2. Wednesday, 30 August 2023 15:16 PM UTC
LOL, that's what I was trying to find out when I asked you:

"You say it fails on "some" machines. Do they all have write access to the drive / folder where you're trying to do the SaveAs() to?"

Glad you solved it!

Please mark as resolved.
  1. Helpful
  1. Chris Pollach @Appeon
  2. Wednesday, 30 August 2023 16:51 PM UTC
Awesome .. I am super glad that you found your problem that caused the issue! :-)
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.