Information générale
Catégorie:
COM/DCOM et OLE Automation
Titre:
IDispatch exception code 0
An Excel automation issue/question ...
I downloaded some code off the FoxWiki and used it for the basis of a function (below) to auto-adjust the widths of columns in a VFP-generated XLS file and also to set the worksheet name.
It works great at the command prompt, but when this function is called from within an SCX I get an error message of "OLE IDispatch exception code 0 from ?: ?..". The error occurs on a property assignment statement (for the worksheet name) as shown below.
If anybody has a suggestion, I'd love to hear it.
Thanks - Doug
*procedure ExcelFix.prg
* Inputs: 1st parameter = filename (existing Excel file)
* 2nd parameter = worksheet name (to be set in the file; optional)
* 3rd parameter = whether to display the file in Excel
* Output: The file is opened, column widths are adjusted, the worksheet name is
* set (if specified) and the file is saved in the latest Excel format.
lparameters lcFilename , lcSheetName , llDisplay
lcSheetName = iif( type('lcSheetName')='C' , lcSheetName , '' )
local lnXlMaximize , lnXlLastCell
lnXlMaximize = -4137
lnXlLastCell = 11
local loExcel
loExcel = CreateObject('Excel.Application')
if vartype(loExcel)<>'O'
ErrMsg( 'Microsoft Excel failed to run!' )
return .F.
endif
loExcel.SheetsInNewWorkBook = 1
loWorkbook = loExcel.Workbooks.Open(lcFilename)
oActiveSheet = loExcel.ActiveSheet
if !empty(lcSheetName)
************** CRASHES ON THE LINE BELOW: ******************
oActiveSheet.Name = lcSheetName
endif
* resize all columns ...
lcLastCell = loExcel.ActiveCell.SpecialCells(lnXlLastCell).Address() && address of last occupied cell
lnMarker1 = at('$',lcLastCell,1) && i.e. 1 when lcLastCell = "$AF$105"
lnMarker2 = at('$',lcLastCell,2) && i.e. 4 when lcLastCell = "$AF$105"
lnStartPos = lnMarker1 + 1
lnStrLen = lnMarker2 - lnStartPos
loExcel.Columns('A:' + substr(lcLastCell,lnStartPos,lnStrLen)).EntireColumn.AutoFit
if llDisplay
loExcel.Application.WindowState = lnXlMaximize
loExcel.visible = .T.
endif
* save Excel file in new Excel format (COPY TO XLS uses old format) ...
loWorkbook.Save()
return
Suivant
Répondre
Voir le fil de ce thread
Voir le fil de ce thread à partir de ce message seulement
Voir tous les messages de ce thread
Voir tous les messages de ce thread à partir de ce message seulement