Skip to content

Instantly share code, notes, and snippets.

@setempler
Created March 8, 2016 11:36
Show Gist options
  • Save setempler/096c689cddd8afd6982b to your computer and use it in GitHub Desktop.
Save setempler/096c689cddd8afd6982b to your computer and use it in GitHub Desktop.
LibreOffice Basic macro code to format all cross reference fields with character style
Function FormatCrossReferences (cStyle As String) As Integer
'field reference and counter
r = "com.sun.star.text.TextField.GetReference"
n = 0
'enumerator for paragraph and section
Dim oParEnum
Dim oSecEnum
'current paragraph and section
Dim oPar
Dim oParSection
oParEnum = thisComponent.Text.createEnumeration()
Do While oParEnum.hasMoreElements()
oPar = oParEnum.nextElement()
If oPar.supportsService("com.sun.star.text.Paragraph") Then
oSecEnum = oPar.createEnumeration()
Do While oSecEnum.hasMoreElements()
oParSection = oSecEnum.nextElement()
If (oParSection.TextPortionType = "TextField") Then
If (oParSection.TextField.SupportedServiceNames(0) = r) then
oParSection.CharStyleName = cStyle
n = n + 1
End If
End if
Loop
End If
Loop
FormatCrossReferences = n
End Function
Sub XRef
'define the character style to apply
'value must exist, otherwise an exception (illegal argument) occurs
s = "Internet Link"
Dim mCurSelection
oDoc = thisComponent
oVC = oDoc.currentController.viewCursor
'store current view cursor
mCurSelection = oDoc.currentSelection
n = FormatCrossReferences(s)
'restore initial cursor position
oDoc.currentController.select(mCurSelection)
MsgBox "Formatted " & n & " cross reference field(s).", 0, "XRef"
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment