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.