I've created the following macros in Calc which loops through a specified row, searches for the pre-defined headers and then deletes their entire column (including the header), the code works, relevant columns do get deleted, however
I've discovered a flaw. In case any of the headers from the pre-defined list are missing from the file I get an error message "Search key not found" and then some other unrelated column gets deleted instead (for e.g. the code is searching for "Text1", "Text2", "Text3" headers however if "Text2" isn't located an error message would pop-up and some unrelated column is deleted).
There should be some kind of validation check that would automatically skip to the next value in the loop if one isn't present in this file.
Conditions:
- Headers row is always 7.
- List item Relevant columns can appear anywhere in the mentioned row.
If the header is located the entire column should be deleted (no
blank column should remain afterwards), if the some headers are missing the code should automatically move on to the next
search value.
I've found some Excel code examples that should do what I need however they are unusable in my case since Calc/Excel code is slightly different.
Any help is greatly appreciated.
sub DeleteSystemFields
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem switch to active cell A7
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$A$7"
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
rem search conditions
dim args4(17) as new com.sun.star.beans.PropertyValue
args4(0).Name = "SearchItem.StyleFamily"
args4(0).Value = 2
args4(1).Name = "SearchItem.CellType"
args4(1).Value = 0
args4(2).Name = "SearchItem.RowDirection"
args4(2).Value = true
args4(3).Name = "SearchItem.AllTables"
args4(3).Value = false
args4(4).Name = "SearchItem.Backward"
args4(4).Value = false
args4(5).Name = "SearchItem.Pattern"
args4(5).Value = false
args4(6).Name = "SearchItem.Content"
args4(6).Value = false
args4(7).Name = "SearchItem.AsianOptions"
args4(7).Value = false
args4(8).Name = "SearchItem.AlgorithmType"
args4(8).Value = 1
args4(9).Name = "SearchItem.SearchFlags"
args4(9).Value = 65536
rem search criteria parameters - corresponds to args4(10) in the next section
args4(10).Name = "SearchItem.SearchString"
args4(10).Value = ""
args4(11).Name = "SearchItem.Locale"
args4(11).Value = 255
args4(12).Name = "SearchItem.ChangedChars"
args4(12).Value = 2
args4(13).Name = "SearchItem.DeletedChars"
args4(13).Value = 2
args4(14).Name = "SearchItem.InsertedChars"
args4(14).Value = 2
args4(15).Name = "SearchItem.TransliterateFlags"
args4(15).Value = 1280
args4(16).Name = "SearchItem.Command"
args4(16).Value = 3
args4(17).Name = "Quiet"
args4(17).Value = true
rem search values start
args4(10).Name = "SearchItem.SearchString"
args4(10).Value = "Text1"
dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())
dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())
args4(10).Name = "SearchItem.SearchString"
args4(10).Value = "Text2"
dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())
dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())
args4(10).Name = "SearchItem.SearchString"
args4(10).Value = "Text3"
dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())
dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())
end sub