LibreOffice Macro mac通过下划线bug找到并替换空格

时间:2015-03-29 05:09:02

标签: replace macros find libreoffice libreoffice-calc

我想在libreoffice中创建一个快捷方式,用下划线替换空格。

我录制了一个宏

我做了一个简单的发现&取代

但每次我尝试运行宏libreoffice退出;(

这是代码

REM  *****  BASIC  *****

sub replacespaces
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 ----------------------------------------------------------------------
dim args1(17) as new com.sun.star.beans.PropertyValue
args1(0).Name = "SearchItem.StyleFamily"
args1(0).Value = 2
args1(1).Name = "SearchItem.CellType"
args1(1).Value = 0
args1(2).Name = "SearchItem.RowDirection"
args1(2).Value = true
args1(3).Name = "SearchItem.AllTables"
args1(3).Value = false
args1(4).Name = "SearchItem.Backward"
args1(4).Value = false
args1(5).Name = "SearchItem.Pattern"
args1(5).Value = false
args1(6).Name = "SearchItem.Content"
args1(6).Value = false
args1(7).Name = "SearchItem.AsianOptions"
args1(7).Value = false
args1(8).Name = "SearchItem.AlgorithmType"
args1(8).Value = 1
args1(9).Name = "SearchItem.SearchFlags"
args1(9).Value = 71680
args1(10).Name = "SearchItem.SearchString"
args1(10).Value = " "
args1(11).Name = "SearchItem.ReplaceString"
args1(11).Value = "_"
args1(12).Name = "SearchItem.Locale"
args1(12).Value = 255
args1(13).Name = "SearchItem.ChangedChars"
args1(13).Value = 2
args1(14).Name = "SearchItem.DeletedChars"
args1(14).Value = 2
args1(15).Name = "SearchItem.InsertedChars"
args1(15).Value = 2
args1(16).Name = "SearchItem.TransliterateFlags"
args1(16).Value = 1280
args1(17).Name = "SearchItem.Command"
args1(17).Value = 3

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SearchResultsDialog", "", 0, Array())


end sub

有人可以告诉我什么是错的吗?

1 个答案:

答案 0 :(得分:2)

openoffice和libreoffice的宏录制器并不像Microsoft Office那样有用。我建议不要使用它。而是记录宏,使用XRAY(https://wiki.documentfoundation.org/Macros)之类的工具来检查使用API​​方法找到的对象。首先检查thisComponent。通过Calc,您将找到一个具有多个工作表模型的模型,每个模型都实现了一个接口XReplaceable(http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1util_1_1XReplaceable.html)。

示例:

Sub findAndReplace()
 oModel = thisComponent ' at first examine thisComponent
 ' xray oModel
 oSpreadSheet = oModel.getCurrentController().getActiveSheet()
 ' oSpreadSheet = oModel.getSheets().getByIndex(0)
 ' xray oSpreadSheet
 xReplaceDescr = oSpreadSheet.createReplaceDescriptor()
 ' xray xReplaceDescr
 xReplaceDescr.SearchString = " "
 xReplaceDescr.ReplaceString = "_"
 lFound = oSpreadSheet.replaceAll(xReplaceDescr)
 ' xray lFound
 MsgBox lFound & " replacements done."
End Sub