'How i can fix: "OLE error Unknown COM status code" in foxpro

I have the code

> lc_nmfp = GETFILE()    IF LEN(ALLTRIM(lc_nmfp)) < 1 then   
> MESSAGEBOX("ôàéë íå âûáðàí")    RETURN    endif ln_w =
> GETWORDCOUNT(lc_nmfp,"\") lc_nmf = GETWORDNUM(lc_nmfp,ln_w,"\") lc_f =
> GETWORDNUM(lc_nmf,1,".") lc_p = "" ln_i = 1    DO while ln_i < ln_w   
> lc_p = ALLTRIM(lc_p)+ALLTRIM(GETWORDNUM(lc_nmfp,ln_i,"\"))+"\"    ln_i
> = ln_i+1    ENDDO
> 
> CREATE CURSOR tmp(H Char(12), N char(2)) CREATE CURSOR
> xlsfile(AccountName CHAR(20), TrafficSourceCommunicationName CHAR(20),
> CommunicationScheduledFor CHAR(20), CommunicationStartDate CHAR(20),
> CommunicationTemplate CHAR(20), Fromv CHAR(12), Tov CHAR(12),
> MessageId CHAR(45),   SendAt CHAR(35), CountryPrefix  CHAR(2),
> CountryName CHAR(16), NetworkName CHAR(21), PurchasePrice CHAR(2),
> Statusv CHAR(25), Reason CHAR(25), Action CHAR(25), ErrorGroup
> CHAR(25), ErrorName CHAR(80), DoneAt CHAR(15), Textv CHAR(254),
> MessagesCount CHAR(2), ServiceName CHAR(16), UserName CHAR(19), SeenAt
> CHAR(19), Clicks CHAR(19), PairedMessageId CHAR(19), DataPayload
> CHAR(19))
> 
> IMPORT FROM lc_p+lc_f+".xls" TYPE XL8 SHEET SData
> 
> SELECT H, N FROM viber_exp WHERE VAL(N) = 1
> 
> 
> 
> 
> 
> oleObject=CREATEOBJECT('EXCEL.Application')  
> oleObject.application.Visible= .T.
> 
> 
> nRow=0
> 
> SCAN      nRow=nRow+1     oleObject.Cells(nRow,1).Value=viber_exp.H
> 
> 
>   oleObject.Cells(nRow,2).Value=viber_exp.N
> 
>    ENDSCAN

It's my code. In line "oleObject.Cells(nRow,1).Value=viber_exp.H" it returns error: "OLE error Unknown COM status code" What am i doing wrong?



Solution 1:[1]

You didn't yet provide any cells. Try adding oleObject.Workbooks.Add() after CreateObject(). Aside from fixing this error, this is not the way for transferring data to Excel (.Value = ...).

Instead use CopyFromRecordSet or QueryTables.Add - (search for VFP2Excel function that handles that for you).

Also import from ... XL8 is also one that you shouldn't use. Check https://foxite.com/archives/0000153776.htm There it is a bit old and now instead you could use CursorAdapter with OleDb or ODBC. ie:

Local loAccess As CursorAdapter,;
  oConn As ADODB.Connection,;
  oRS As ADODB.Recordset, ;
  oException As Exception, ;
  cConnString As String, ;
  lcSheetName

lcDataSource = 'c:\myFoler\MyFile.xlsx'
lcSheetName = 'Sheet1'

cConnString = ;
    'Provider=Microsoft.ACE.OLEDB.12.0;' + ;
    'Data Source=' + m.lcDataSource + ';' + ;
    'Extended Properties="Excel 12.0;HDR=Yes;IMEX=0"'
    
* Handle connections - insert connection code

Try
  oConn  = Createobject('ADODB.Connection')
  oConn.Open(cConnString)

  oRS = Createobject("ADODB.Recordset")
*!*   oRS.Datasource.CursorLocation = 3   &&adUseClient
*!*   oRS.Datasource.LockType = 3   &&adLockOptimistic
  oRS.ActiveConnection = oConn

  oCA=Createobject("CursorAdapter")
  oCA.DataSourceType = "ADO"
  oCA.FetchAsNeeded = .F.
  oCA.Datasource = oRS
  oCA.MapBinary = .T.
  oCA.MapVarchar = .T.

  oCA.Alias = "SampleData"
  oCA.SelectCmd = "SELECT * FROM ["+m.lcSheetName+"$]"

  If !oCA.CursorFill()
    Local array laError[5]
    Aerror(laError)
    Messagebox(laError[2])
  Else
    Browse Normal
  Endif

Catch To oException
  * Replace with exception handling code here
  Messagebox(oException.Message)
Endtry

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cetin Basoz