I am attempting to create a LibreOffice Basic macro for use in Calc that when invoked in an open spreadsheet file will open a second spreadsheet file that contains two ranges, (the first range containing cells with regular expressions to search for and the second containing the replacement text) then will search in the sheet it was invoked from for all the items in the search range and replace any items found in the working sheet with the corresponding replacement cell including cell the formatting of the replacement cell. I asked ChatGPT for help and the following is in general what it supplied, with some corrections that I made to the logic that determines the size of the range.
Sub SearchAndReplaceItems
Dim oDoc As Object
Dim oSearchReplaceDoc As Object
Dim oSheet As Object
Dim oSearchReplaceSheet As Object
Dim oSearchDescriptor As Object
Dim oSearchRange As Object
Dim oReplaceDescriptor As Object
Dim oCell As Object
Dim oFound As Object
Dim nSearchColumn As Integer
Dim nReplaceColumn As Integer
Dim nLastRow As Integer
' Get the current document
oDoc = ThisComponent
' Specify the path to the Corrections spreadsheet
Dim sSearchReplaceFilePath As String
sSearchReplaceFilePath = "C:\Users\One\Info\Corrections.ods"
' Open the Corrections spreadsheet
oSearchReplaceDoc = StarDesktop.loadComponentFromURL(ConvertToURL(sSearchReplaceFilePath), "_blank", 0, Array())
oSearchReplaceSheet = oSearchReplaceDoc.Sheets(0)
' Specify the column numbers for search and replace text
nSearchColumn = 1 ' Assuming search text is in column A
nReplaceColumn = 2 ' Assuming replace text is in column B
' Get the range for the search regular expressions
oSearchRange = oSearchReplaceDoc.Sheets(0).getCellRangeByName("SearchRegExps")
' Get the last row with data in the SearchRegExps range
nLastRow = oSearchRange.Rows.getCount()
' Loop through each row History Item Corrections sheet
For i = 1 To nLastRow
' Get search and replace values from the current row
Dim sSearchText As String
Dim sReplaceText As String
sSearchText = oSearchReplaceSheet.getCellByPosition(nSearchColumn - 1, i).getString()
sReplaceText = oSearchReplaceSheet.getCellByPosition(nReplaceColumn - 1, i).getString()
' Create search and replace descriptors
oSearchDescriptor = oDoc.createSearchDescriptor()
oSearchDescriptor.SearchString = sSearchText
oReplaceDescriptor = oDoc.createReplaceDescriptor()
oReplaceDescriptor.ReplaceString = sReplaceText
' Execute the search
oFound = oDoc.createReplaceDescriptor()
oFound = oDoc.findFirst(oSearchDescriptor)
' Replace each occurrence found
Do While Not IsNull(oFound)
oCell = oFound.Cell
oCell.setString(sReplaceText)
' Copy formatting from replace cell to the found cell
CopyCellFormatting(oCell, oFound.Cell)
' Find the next occurrence
oFound = oDoc.findNext(oFound)
Loop
Next i
' Close the search and replace document
oSearchReplaceDoc.Close(True)
End Sub
Sub CopyCellFormatting(oSourceCell, oTargetCell)
' Copy character formatting
oTargetCell.CharFontName = oSourceCell.CharFontName
oTargetCell.CharHeight = oSourceCell.CharHeight
oTargetCell.CharWeight = oSourceCell.CharWeight
oFound.Cell.CharAutoKerning = oCell.CharAutoKerning
oFound.Cell.CharKerning = oCell.CharKerning
oFound.Cell.CharScaleWidth = oCell.CharScaleWidth
' Copy border formatting
oTargetCell.BottomBorder = oSourceCell.BottomBorder
oTargetCell.TopBorder = oSourceCell.TopBorder
oTargetCell.LeftBorder = oSourceCell.LeftBorder
oTargetCell.RightBorder = oSourceCell.RightBorder
End Sub
When I run the macro the error
BASIC runtime error. Property or method not found: createSearchDescriptor.
occurs on line 50 of the macro, which is:
oSearchDescriptor = oDoc.createSearchDescriptor()
I comprehend that the object oDoc (the document that the macro was invoked from) does not have a property or method named createSearchDescriptor, but I don't know enough LibreOffice Basic to figure what it should be doing here so that it will work. I looked through Andrew Pitonyak's book "Useful Macro Information For OpenOffice.org" and his "OpenOffice.org Macros Explained" book but did not find anything in them about using SearchDescriptors.
I suspect that a similar error would occur if the statement on line 53
oReplaceDescriptor = oDoc.createReplaceDescriptor()
were to be executed.
It would not surprise me if there were other errors in this code that will prevent it from running successfully.
I would greatly appreciate any insight that can be provided into how I can achieve what I desire to accomplish with this macro.
.createSearchDescriptor()
method, every sheet of the spreadsheet has this method. By the way, how many sheets are in your modifiable workbook? Should the replacement be carried out on each of these sheets or only on the first? (Seeking help from the AI was not the best way to solve the problem)