目前我使用的是LibreOffice 4.2.4.2。经过以往的测试和使用经验,这是诸多版本中较为稳定和bug相对较少的。今天无意中发现该版本的LibreOffice Draw存在一个问题:样式名称修改后无法保存。于是,我便想到,既然在图形界面无法保存修改后的样式名称,那么能否用LibreOffice Basic宏命令试试看。事实证明,使用下面的代码则可以实现该功能。该函数会让用户依次指定欲修改样式所属的类别(Family)、样式名称、新的样式名称。由此也可以看出,LibreOffice也许在图形界面上存在一些bug,但是若通过宏命令直接调用底层的API,则可以有效地作用于文档,对相应的bug也是一个弥补。
' Change a style name with a new name. This solves the problem of changed style name in Draw cannot be saved.
Sub ChangeStyleNames
Dim style_family_name As String
Dim style_name As String
Dim style_new_name As String 'Names of styles. Array of string
Dim oStyles
'Styles is interface com.sun.star.container.XNameAccess
Dim oStyle style_family_name = InputBox("Style family name: ") If style_family_name <> "" Then
If ThisComponent.StyleFamilies.hasByName(style_family_name) Then
oStyles = ThisComponent.StyleFamilies.getByName(style_family_name) style_name = InputBox("Style name: ") If style_name <> "" Then
If oStyles.hasByName(style_name) Then
oStyle = oStyles.getByName(style_name) style_new_name = InputBox("New style name: ") If style_new_name <> "" And style_new_name <> style_name Then
oStyle.setName(style_new_name)
Else
MsgBox "Please specify a new style name!", 0, "Warning"
End If
Else
MsgBox "Cannot find the style: " + "test", 0, "Warning"
End If
Else
MsgBox "Please specify a style name!", 0, "Warning"
End If
Else
MsgBox "Style family name is invalid!", 0, "Error"
End if
Else
MsgBox "Please specify a style family name!", 0, "Warning"
End if
End Sub