FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Ocalc:how to open an exist file?
Posts: 474
Joined: Sun Oct 30, 2005 06:37 AM
Ocalc:how to open an exist file?
Posted: Fri Jul 04, 2025 01:04 AM

Hi,

cfile:="d:\tmp\my.xls")

if (oCalc := SunCalcObj()) <> nil

       oDesktop := oCalc:CreateInstance( "com.sun.star.frame.Desktop" )

       aprop:={}

                 AAdd(aProp,GetPropertyValue(oCalc, "Hidden", .T. )  )

       oBook    := oDesktop:LoadComponentFromURL(ALLTRIM(cfile) , "_blank", 0, aprop )



               oSheet   := oBook:GetSheets():GetByIndex( 0 )

end

// LoadComponentFromURL(ALLTRIM(cfile), argument error

Tks !

Shuming Wang

http://www.xtech2.top
Mobile:(86)13802729058
Email:100200651@qq.com
QQ:100200651
Weixin: qq100200651
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Ocalc:how to open an exist file?
Posted: Fri Jul 04, 2025 03:36 AM
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 474
Joined: Sun Oct 30, 2005 06:37 AM
Re: Ocalc:how to open an exist file?
Posted: Sun Jul 06, 2025 09:02 AM

Thank you! But I can't open this China mainland forbidden site.

Shuming Wang

http://www.xtech2.top
Mobile:(86)13802729058
Email:100200651@qq.com
QQ:100200651
Weixin: qq100200651
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: Ocalc:how to open an exist file?
Posted: Mon Jul 07, 2025 01:44 PM
   cfile := "d:\tmp\my.xls" )

   IF ( oCalc := SunCalcObj() ) <> nil

      oDesktop := oCalc:CreateInstance( "com.sun.star.frame.Desktop" )

      aprop := {}

      AAdd( aProp, GetPropertyValue( oCalc, "Hidden", .T. ) )

      oBook  := oDesktop:LoadComponentFromURL( AllTrim( cfile ), "_blank", 0, aprop )
      oSheet := oBook:GetSheets():GetByIndex( 0 )

   END

   // LoadComponentFromURL(ALLTRIM(cfile), argument error

   The error LoadComponentFromURL( AllTrim( cfile ), argument error in your CODE suggests an issue WITH the arguments passed TO the LoadComponentFromURL METHOD in the OpenOffice / LibreOffice Basic ( StarBasic ) or Automation context. This METHOD is used TO load a document ( in this CASE, an Excel FILE my.xls ) into the office suite. Let's analyze the issue and provide a solution.
   Problem Analysis
   The LoadComponentFromURL METHOD has the following signature:

   vb
   oDesktop:LoadComponentFromURL( sURL, sTargetFrameName, nSearchFlags, aArgs )
   sURL: The FILE PATH converted TO a URL FORMAT ( e .g., file:// /d:/tmp/my.xls).
   sTargetFrameName: The name of the frame where the document should be loaded ( e .g., "_blank" FOR a NEW frame ).
   nSearchFlags: An integer specifying search options ( usually 0 FOR no special flags ).
   aArgs: An array of property values ( e .g., FOR options LIKE Hidden ).
   The error likely stems FROM one of the following issues:
   Invalid URL format: The FILE PATH d:\tmp\my.xls needs TO be converted TO a proper file:// URL format.
   Incorrect array FOR aProp: The aProp array may not be correctly structured or may contain invalid property values.
   CASE sensitivity or METHOD availability: The METHOD or property names might be case - sensitive or not properly initialized.
   FILE PATH issues: The FILE PATH may not exist, or there may be permission issues.
   Corrected CODE
   Here's a corrected version of your code, addressing the potential issues:

   vb
   cfile := "file:///d:/tmp/my.xls"  // Convert to file URL format

   IF ( oCalc := SunCalcObj() ) <> nil

      oDesktop := oCalc:CreateInstance( "com.sun.star.frame.Desktop" )
      aProp    := {}  // Array for properties

      AAdd( aProp, GetPropertyValue( oCalc, "Hidden", .T. ) )  // Add Hidden property

      oBook    := oDesktop:LoadComponentFromURL( cfile, "_blank", 0, aProp )

      IF oBook <> nil
         oSheet := oBook:GetSheets():GetByIndex( 0 )
      ELSE
         MsgBox( "Failed to load the document!" )
      ENDIF

   ENDIF

   KEY Fixes and Explanations

   FILE URL Format:

   Changed cfile := "d:\tmp\my.xls" TO cfile := "file:///d:/tmp/my.xls". The LoadComponentFromURL METHOD requires a proper URL FORMAT. FOR LOCAL files, USE file:// / followed by the path, with forward slashes (/) instead of backslashes (\).

   Example: d:\tmp\my.xls becomes file:// /d:/tmp/my.xls.

   Trimming the FILE Path:
   The original CODE used AllTrim( cfile ). Ensure that cfile does not have leading / trailing spaces. However, since the FILE PATH is hardcoded, ALLTRIM may not be necessary unless cfile is dynamically generated.
   Property Array ( aProp ):
   The GetPropertyValue FUNCTION is assumed TO CREATE a com .sun. star .beans. PropertyValue STRUCTURE. Ensure this FUNCTION is correctly defined. A typical implementation looks LIKE this:

   vb

   FUNCTION GetPropertyValue( oService, sName As String, vValue As Variant ) As Object

      Dim oProp As Object
      oProp = oService.CreateInstance( "com.sun.star.beans.PropertyValue" )
      oProp.Name = sName
      oProp.Value = vValue
      GetPropertyValue = oProp

   End Function

   The Hidden property SET TO .T. ( true ) ensures the document is loaded without displaying the UI, which is VALID FOR background processing.

   Error Handling:

   Added a CHECK TO ensure oBook is not NIL before accessing the sheets. This helps catch errors IF the document fails TO load ( e .g., FILE not found or invalid format ).

   CASE Sensitivity:

   Ensure METHOD and service names LIKE com .sun. star .frame. Desktop and LoadComponentFromURL match exactly, as OpenOffice / LibreOffice APIs are case - sensitive.

   Additional Considerations

   FILE Existence: Verify that the FILE d:\tmp\my.xls exists and is accessible. IF the FILE is missing or the PATH is incorrect, the METHOD will fail.
   FILE Format: Ensure my.xls is a VALID Excel FILE compatible WITH OpenOffice / LibreOffice. IF it's a newer .xlsx FORMAT, ensure the office suite supports it.

   Permissions: CONFIRM that the application has READ permissions FOR d:\tmp\my .xls.
   SunCalcObj: The SunCalcObj FUNCTION is assumed TO RETURN a VALID service manager object. Ensure it is correctly implemented and returns a non - NIL object.
   Debugging: IF the error persists, add error handling TO capture the specific exception:

   vb

   Try

      oBook := oDesktop:LoadComponentFromURL( cfile, "_blank", 0, aProp )

   Catch e

      MsgBox( "Error loading document: " + e.Message )

   End Try

   Testing the CODE

   Verify the FILE PATH and convert it TO a file:// / URL.

   Ensure the GetPropertyValue FUNCTION is correctly defined.

   RUN the CODE in an environment WITH OpenOffice / LibreOffice installed.

   CHECK the CONSOLE or MESSAGE BOX FOR any error messages.

   IF you still encounter issues, please provide:

      The EXACT error MESSAGE.
      The definition of the SunCalcObj and GetPropertyValue functions.
      Details about the environment ( e .g., OpenOffice or LibreOffice version ).
      This should resolve the argument error and allow the document TO load correctly.
Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1096
Joined: Fri Oct 28, 2005 02:27 AM
Re: Ocalc:how to open an exist file?
Posted: Tue Jul 08, 2025 04:24 AM
ShumingWang wrote: Thank you! But I can't open this China mainland forbidden site.
Shuming Wang
I copy paste for you from Grok:

The error LoadComponentFromURL(ALLTRIM(cfile), argument error in your code suggests an issue with the arguments passed to the LoadComponentFromURL method in the OpenOffice/LibreOffice Basic (StarBasic) or Automation context. This method is used to load a document (in this case, an Excel file my.xls) into the office suite. Let’s analyze the issue and provide a solution.

Problem Analysis
The LoadComponentFromURL method has the following signature:
vb
oDesktop:LoadComponentFromURL(sURL, sTargetFrameName, nSearchFlags, aArgs)
  • sURL: The file path converted to a URL format (e.g., file:///d:/tmp/my.xls).
  • sTargetFrameName: The name of the frame where the document should be loaded (e.g., "_blank" for a new frame).
  • nSearchFlags: An integer specifying search options (usually 0 for no special flags).
  • aArgs: An array of property values (e.g., for options like Hidden).
The error likely stems from one of the following issues:
  • Invalid URL format: The file path d:\tmp\my.xls needs to be converted to a proper file:// URL format.
  • Incorrect array for aProp: The aProp array may not be correctly structured or may contain invalid property values.
  • Case sensitivity or method availability: The method or property names might be case-sensitive or not properly initialized.
  • File path issues: The file path may not exist, or there may be permission issues.

Corrected Code
Here’s a corrected version of your code, addressing the potential issues:
vb
cfile := "file:///d:/tmp/my.xls"  // Convert to file URL format
if (oCalc := SunCalcObj()) <> nil
    oDesktop := oCalc:CreateInstance("com.sun.star.frame.Desktop")
    aProp := {}  // Array for properties
    AAdd(aProp, GetPropertyValue(oCalc, "Hidden", .T.))  // Add Hidden property
    oBook := oDesktop:LoadComponentFromURL(cfile, "_blank", 0, aProp)
    
    if oBook <> nil
        oSheet := oBook:GetSheets():GetByIndex(0)
    else
        MsgBox("Failed to load the document!")
    endif
endif
Key Fixes and Explanations
  • File URL Format:
  • Changed cfile := "d:\tmp\my.xls" to cfile := "file:///d:/tmp/my.xls". The LoadComponentFromURL method requires a proper URL format. For local files, use file:/// followed by the path, with forward slashes (/) instead of backslashes (\).
  • Example: d:\tmp\my.xls becomes file:///d:/tmp/my.xls.
  • Trimming the File Path:
  • The original code used ALLTRIM(cfile). Ensure that cfile does not have leading/trailing spaces. However, since the file path is hardcoded, ALLTRIM may not be necessary unless cfile is dynamically generated.
  • Property Array (aProp):
  • The GetPropertyValue function is assumed to create a com.sun.star.beans.PropertyValue structure. Ensure this function is correctly defined. A typical implementation looks like this:
    vb
    Function GetPropertyValue(oService, sName As String, vValue As Variant) As Object
        Dim oProp As Object
        oProp = oService.CreateInstance("com.sun.star.beans.PropertyValue")
        oProp.Name = sName
        oProp.Value = vValue
        GetPropertyValue = oProp
    End Function
  • The Hidden property set to .T. (true) ensures the document is loaded without displaying the UI, which is valid for background processing.
Error Handling:
  • Added a check to ensure oBook is not nil before accessing the sheets. This helps catch errors if the document fails to load (e.g., file not found or invalid format).
Case Sensitivity:
  • Ensure method and service names like com.sun.star.frame.Desktop and LoadComponentFromURL match exactly, as OpenOffice/LibreOffice APIs are case-sensitive.
Additional Considerations
  • File Existence: Verify that the file d:\tmp\my.xls exists and is accessible. If the file is missing or the path is incorrect, the method will fail.
  • File Format: Ensure my.xls is a valid Excel file compatible with OpenOffice/LibreOffice. If it’s a newer .xlsx format, ensure the office suite supports it.
  • Permissions: Confirm that the application has read permissions for d:\tmp\my.xls.
  • SunCalcObj: The SunCalcObj function is assumed to return a valid service manager object. Ensure it is correctly implemented and returns a non-nil object.
  • Debugging: If the error persists, add error handling to capture the specific exception:
    vb
    Try
        oBook := oDesktop:LoadComponentFromURL(cfile, "_blank", 0, aProp)
    Catch e
        MsgBox("Error loading document: " + e.Message)
    End Try
Testing the Code
  • Verify the file path and convert it to a file:/// URL.
  • Ensure the GetPropertyValue function is correctly defined.
  • Run the code in an environment with OpenOffice/LibreOffice installed.
  • Check the console or message box for any error messages.
If you still encounter issues, please provide:
  • The exact error message.
  • The definition of the SunCalcObj and GetPropertyValue functions.
  • Details about the environment (e.g., OpenOffice or LibreOffice version).
This should resolve the argument error and allow the document to load correctly.
FWH 11.08/FWH 19.12

BCC5.82/BCC7.3

xHarbour/Harbour
Posts: 474
Joined: Sun Oct 30, 2005 06:37 AM
Re: Ocalc:how to open an exist file?
Posted: Wed Jul 09, 2025 03:01 AM

It works!

cfile:="file:///"+StrTran(Alltrim(cfile),"\","/")

// can convert like "d:\tmp\file1.xls" , "\127.0.0.1\tmp\file1.xls" into url .

Thank Antonio,karinha,hua!

Shuming Wang

http://www.xtech2.top
Mobile:(86)13802729058
Email:100200651@qq.com
QQ:100200651
Weixin: qq100200651

Continue the discussion