Global Const endnoteFieldPrefix = "custom_endnote_" Global Const endnoteInTextAnchorSuffix = "-text_anchor" Global Const endnoteInBodyAnchorSuffix = "-body_anchor" Sub convertEndnotesExecution Dim oSelections As Object Dim oAnchor1 As Object Dim oAnchor2 As Object Dim oAnchor1Name As String Dim oAnchor2Name As String Dim foundEndNotes() As Object Dim textSelection As Object Dim nativeEndnotesCounter As Integer Dim customEndnotesCounter As Integer If IsNull(ThisComponent) Then MsgBox getTranslation("something weird happened error text") Exit Sub End If oSelections = ThisComponent.getCurrentSelection() If NOT oSelections.supportsService("com.sun.star.text.TextRanges") Then MsgBox getTranslation("EndnotesNotFound") Exit Sub End If objectsCount = oSelections.getCount() If objectsCount > 1 Then MsgBox getTranslation("too much selections") Exit Sub End If textSelection = oSelections.getByIndex(0) foundEndNotes() = findEndNotesInSelection(textSelection) nativeEndnotesCounter = UBound(foundEndNotes(0)) customEndnotesCounter = UBound(foundEndNotes(1)) If (nativeEndnotesCounter = -1 And customEndnotesCounter = -1 ) Then MsgBox getTranslation("EndnotesNotFound") Exit Sub EndIf If ( customEndnotesCounter = -1 ) Then 'only native found window convert to custom? 'Window Select paragraph for endnotes output or cancel runEndnotesConversionDialog(foundEndNotes(0)) Exit Sub EndIf If (nativeEndnotesCounter = -1 ) Then 'only custom endnotes found. Window convert to native? Exit Sub EndIf 'both custom and native endnotes found 'convert to custom all or 'convert to native End Sub Sub runEndnotesConversionDialog(foundEndNotes As Variant) Dim dialog As Object waitingForDialog = true dialog = notModalDialog("EndnotesConversion") dialog.getControl("found").SetText(getTranslation("EndnotesNativeDialogFound") & CStr(UBound(foundEndNotes)+1)) dialog.getControl("description").SetText(getTranslation("EndnotesNativeDialogDescriptionSelect")) dialog.getControl("cancel").Label = getTranslation("buttonCancel") dialog.getControl("start").Label = getTranslation("buttonOk") dialog.setvisible(true) Do While waitingForDialog If dialog.getControl("cancel").model.state = 1 then exit Do EndIf If dialog.getControl("start").model.state = 1 then convertEndnotesToCustom(foundEndNotes) exit Do EndIf wait (100) Loop dialog.dispose End Sub Sub convertEndnotesToCustom(foundEndNotes As Variant) Dim oViewCursor As Object Dim outputPosition As Object Dim endnoteSigns() As String Dim fieldPrefix As String Dim i As Integer oViewCursor = ThisComponent.CurrentController.getViewCursor() oViewCursor.goToStartOfLine(false) outputPosition = oViewCursor.Text.createTextCursorByRange(oViewCursor) endnoteSigns = getEndnoteSigns(foundEndNotes) fieldPrefix = createEndnoteFieldPrefix() For i = LBound(foundEndNotes) To Ubound(foundEndNotes) convertNativeEndnoteToCustom(endnoteSigns(i), foundEndNotes(i),outputPosition,fieldPrefix, i) Next i End Sub Function createEndnoteFieldPrefix As String Dim names() As String Dim i As Integer Dim prefix As String names = ThisComponent.getTextFieldMasters().getElementNames() For i = 0 To 10000 prefix = endnoteFieldPrefix & i & "_" If NOT startsWithInArray(prefix, names) Then createEndnoteFieldPrefix = prefix Exit Function EndIf Next i createEndnoteFieldPrefix = prefix End Function Function startsWithInArray(prefix As String, names As Variant) As Boolean Dim i As Long For i = LBound(names) To Ubound(names) If InStr(names(i), "com.sun.star.text.fieldmaster.User." & prefix) = 1 Then startsWithInArray = true Exit Function EndIf Next i startsWithInArray = false End Function Sub convertNativeEndnoteToCustom(sign As String, nativeEndNoteTextRange As Object,outputPosition As Object, fieldPrefix As String, num As Integer) 'Globalscope.BasicLibraries.LoadLibrary( "MRILib" ) Dim oViewCursor As Object Dim oTextCursor As Object Dim endNote As Object Dim anchorInBody As Object Dim anchorInText As Object Dim bodyAnchorName As String Dim textAnchorName As String endNote = nativeEndNoteTextRange.Footnote oViewCursor = ThisComponent.CurrentController.getViewCursor() oViewCursor.goToRange(nativeEndNoteTextRange.Footnote.Text.Start,false) oViewCursor.goToRange(nativeEndNoteTextRange.Footnote.Text.End,true) unoCut() oViewCursor.goToRange(outputPosition,false) getEndnotePara() oViewCursor.ParaStyleName = "Endnote" anchorInBody = createEndnoteSign(outputPosition, sign, fieldPrefix & num, true) addEndnoteSpacer(fieldPrefix) unoPaste() outputPosition = oViewCursor.End oViewCursor.goToRange(nativeEndNoteTextRange,false) endNote.dispose() anchorInText = createEndnoteSign(oViewCursor, sign, fieldPrefix & num, false) bodyAnchorName = fieldPrefix & num & endnoteInBodyAnchorSuffix textAnchorName = fieldPrefix & num & endnoteInTextAnchorSuffix anchorInBody.goRight(1,true) anchorInText.goRight(1,true) createAnchor(anchorInBody, bodyAnchorName ) createAnchor(anchorInText,textAnchorName ) createLink(anchorInBody,anchorInBody.String, textAnchorName) createLink(anchorInText,anchorInText.String, bodyAnchorName) End Sub Sub getEndnotePara() Dim oViewCursor As Object Dim oTextCursor As Object oViewCursor = ThisComponent.CurrentController.getViewCursor() oTextCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor.Start) oTextCursor.gotoStartOfParagraph(false) oTextCursor.gotoEndOfParagraph(true) If Len(oTextCursor.String) > 0 Then oViewCursor.Text.insertControlCharacter(oViewCursor.End,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False) EndIf End Sub Function createEndnoteSign(cursor As Object,sign As String,fieldName As String, inBody As Boolean) As Object Dim oField As Object 'Field to insert Dim oFieldMaster As Object Dim oTextCursor As Object Dim oMasters As Object oTextCursor = cursor.Text.createTextCursorByRange(cursor.Start) If inBody Then oTextCursor.CharStyleName = "Endnote Symbol" Else oTextCursor.CharStyleName = "Endnote anchor" EndIf oField = ThisComponent.createInstance("com.sun.star.text.textfield.User") oMasters = ThisComponent.getTextFieldMasters() If oMasters.hasByName("com.sun.star.text.FieldMaster.User" & "." & fieldName) Then oFieldMaster = oMasters.getByName("com.sun.star.text.FieldMaster.User" & "." & fieldName) oFieldMaster.Name = fieldName oFieldMaster.Content = sign Else oFieldMaster = ThisComponent.createInstance("com.sun.star.text.FieldMaster.User") oFieldMaster.Name = fieldName oFieldMaster.Content = sign EndIf oField.attachTextFieldMaster(oFieldMaster) oTextCursor.Text.insertTextContent(oTextCursor, oField, False) oTextCursor.CharStyleName = "Standard" oTextCursor.goLeft(1,false) createEndnoteSign = oTextCursor End Function Sub addEndnoteSpacer(fieldPrefix As String) Dim oViewCursor As Object oViewCursor = ThisComponent.CurrentController.getViewCursor() insertUserField(oViewCursor,fieldPrefix & "spacer"," ") End Sub Function getEndnoteSigns(foundEndNotes As Variant) As Variant Dim i As Integer Dim signs() As String For i = LBound(foundEndNotes) To Ubound(foundEndNotes) AddToArray(signs, foundEndNotes(i).String) Next i getEndnoteSigns = signs End Function Function notModalDialog(dialogName As String) As Variant Dim windowProvider As Object Dim containerWindow As Object Dim handler As Object Dim dialogUrl As String Dim dialog As Object containerWindow = ThisComponent.getCurrentController().getFrame().getContainerWindow() dialogUrl = "vnd.sun.star.script:ePublishing." & dialogName & "?location=application" windowProvider = CreateUnoService("com.sun.star.awt.ContainerWindowProvider") dialog = windowProvider.createContainerWindow(dialogUrl, "", containerWindow, handler) notModalDialog = dialog End Function Function findEndNotesInSelection(textSelection) As Variant Dim nativeEndNotes() As Object Dim customEndNotes() As Object Dim outerArray() As Object findEndNotesInSelection = Array() Dim enum1Element As Object Dim cellEnumElement As Object Dim enum1 As Object Dim enum2 As Object Dim curNum As Integer Dim i As Integer Dim cell As Object Dim cellEnum As Object enum1 = textSelection.createEnumeration While enum1.hasMoreElements enum1Element = enum1.nextElement If enum1Element.supportsService("com.sun.star.text.Paragraph") Then findEndNotesInParagraph(enum1Element, nativeEndNotes, customEndNotes) ElseIf enum1Element.supportsService("com.sun.star.text.TextTable") Then cellNames = enum1Element.cellNames For i = LBound(cellNames) To Ubound(cellNames) cell = enum1Element.getCellByName(cellNames(i)) cellEnum = cell.getText().createEnumeration() While cellEnum.hasMoreElements cellEnumElement = cellEnum.nextElement If cellEnumElement.supportsService("com.sun.star.text.Paragraph") Then findEndNotesInParagraph(cellEnumElement, nativeEndNotes, customEndNotes) EndIf Wend Next i EndIf Wend addToArray(outerArray, nativeEndNotes) addToArray(outerArray, customEndNotes) findEndNotesInSelection = outerArray End Function Sub findEndNotesInParagraph(enum1Element As Object,nativeEndNotes As Variant, customEndNotes As Variant) Dim textPortions As Object Dim thisPortion As Object Dim footnoteText As Object Dim label As String Dim labelNum As Long If enum1Element.OutlineLevel = level Then curNum = 1 EndIf textPortions = enum1Element.createEnumeration While textPortions.hasMoreElements thisPortion = textPortions.nextElement If isTargetNote(thisPortion,1) Then AddToArray(nativeEndNotes, thisPortion) ' ElseIf isCustomEndnote(thisPortion) ' AddToArray(customEndNotesd, thisPortion) EndIf Wend End Sub