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? runEndnotesCustomToNativeDialog(foundEndNotes(1)) Exit Sub EndIf 'both custom and native endnotes found 'convert to custom all or 'convert to native runDualEndnotesDialog(foundEndNotes(0),foundEndNotes(1)) End Sub Sub runDualEndnotesDialog(nativeEndNotes() As Object,customEndnotes() As Object) Dim dialog As Object waitingForDialog = true dialog = notModalDialog("EndnotesConversion") dialog.getControl("found").SetText(getTranslation("EndnotesNativeDialogFound") & CStr(UBound(nativeEndNotes)+1)) dialog.getControl("found2").SetText(getTranslation("EndnotesCustomDialogFound") & CStr(UBound(customEndnotes)+1)) dialog.getControl("found2").Model.EnableVisible = TRUE dialog.getControl("description").SetText(getTranslation("EndnotesConversionTwoOptions")) dialog.getControl("cancel").Label = getTranslation("buttonCancel") dialog.getControl("start").Label = getTranslation("toNativeEndnotes") dialog.getControl("option2").Label = getTranslation("toCustomEndnotes") dialog.getControl("option2").Model.EnableVisible = TRUE 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 convertCustomEndnotesToNative(customEndnotes) exit Do EndIf If dialog.getControl("option2").model.state = 1 then convertEndnotesToCustom(nativeEndNotes) exit Do EndIf wait (100) Loop dialog.dispose End Sub Sub runEndnotesCustomToNativeDialog(customEndnotes() As Object) Dim dialog As Object waitingForDialog = true dialog = notModalDialog("EndnotesConversion") dialog.getControl("found").SetText(getTranslation("EndnotesCustomDialogFound") & CStr(UBound(customEndnotes)+1)) dialog.getControl("description").SetText(getTranslation("EndnotesCustomConversionOption")) 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 convertCustomEndnotesToNative(customEndnotes) exit Do EndIf wait (100) Loop dialog.dispose End Sub Sub convertCustomEndnotesToNative(foundEndnotes() As Object) Dim i As Long Dim bodyAnchorName As String Dim textAnchorName As String Dim bookmarksName As String Dim bookmarks As Object bookmarksName = ThisComponent.Links.ElementNames(6) bookmarks = ThisComponent.Links.getByName(bookmarksName) For i = LBound(foundEndnotes) To UBound(foundEndnotes) bodyAnchorName = getBodyAnchorName(foundEndnotes(i)) textAnchorName = getTextAnchorName(bodyAnchorName) If bookmarks.hasByName(bodyAnchorName) And bookmarks.hasByName(textAnchorName) Then convertLinksToEndnote(bodyAnchorName,textAnchorName) EndIf Next i End Sub Function getBodyAnchorName(textRange As Object) As String getBodyAnchorName = "" If (Len(textRange.HyperLinkURL) > 0) Then getBodyAnchorName = Right(textRange.HyperLinkURL,Len(textRange.HyperLinkURL)-1) EndIf End Function Function getTextAnchorName(bodyAnchorName As String) As String getTextAnchorName = "" If Len(bodyAnchorName) > Len(endnoteInBodyAnchorSuffix) Then getTextAnchorName = Left(bodyAnchorName,Len(bodyAnchorName)-Len(endnoteInBodyAnchorSuffix)) & endnoteInTextAnchorSuffix EndIf End Function Sub convertLinksToEndnote(forwardLink As String,backwardLink As String) Dim bookMarkName As String bookmarkName = ThisComponent.Links.ElementNames(6) Dim bookmarks As Object bookmarks = ThisComponent.Links.getByName(bookmarkName) Dim forward As Object Dim backward As Object Dim oViewCursor As Object Dim footNoteSign As String oViewCursor = ThisComponent.CurrentController.getViewCursor() Dim oTextCursor As Object If NOT bookmarks.hasByName(forwardLink) OR NOT bookmarks.hasByName(backwardLink) Then exit sub EndIf forward = bookmarks.getByName(forwardLink) backward = bookmarks.getByName(backwardLink) footNoteSign = forward.Anchor.String oViewCursor.goToRange(forward.Anchor,false) SendRM SendRM oTextCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor) goToEndOfEndnoteBody(oTextCursor) oTextCursor.goToRange(oViewCursor,true) oViewCursor.goToRange(oTextCursor,true) unoCut() SendRM oViewCursor.goToRange(backward.Anchor,false) removeEndnoteSignInText() createEndnote unoPaste() oViewCursor.getText.setLabel(footNoteSign) forward.dispose() backward.dispose() End Sub Sub goToEndOfEndnoteBody(oTextCursor As Object) Dim position As Object position = oTextCursor.End oTextCursor.goToEndOfParagraph(false) oTextCursor.goRight(1,false) If oTextCursor.ParaStyleName <> "Endnote" OR oTextCursor.CharStyleName = "Endnote Symbol" Then oTextCursor.goLeft(1,false) Exit Sub EndIf 'Got to the end of the document If oTextCursor.Text.compareRegionEnds(position,oTextCursor.End) = 0 Then Exit Sub EndIf goToEndOfEndnoteBody(oTextCursor) End Sub Sub removeEndnoteSignInText() Dim oViewCursor As Object Dim character As String oViewCursor = ThisComponent.CurrentController.getViewCursor() oViewCursor.String = "" oViewCursor.HyperLinkURL="" End Sub sub createEndnote 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") dispatcher.executeDispatch(document, ".uno:InsertEndnote", "", 0, Array()) 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 prefix As String names = ThisComponent.getTextFieldMasters().getElementNames() prefix = endnoteFieldPrefix & RND_String() & "_" If NOT startsWithInArray(prefix, names) Then createEndnoteFieldPrefix = prefix Exit Function EndIf 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) If oViewCursor.String = "" Then oViewCursor.String = " " EndIf 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 Dim bookmarkName As String 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) Then AddToArray(customEndNotes, thisPortion) EndIf Wend End Sub Function isCustomEndnote(portion As Object) As Boolean If InStr(portion.HyperLinkURL, "#" & endnoteFieldPrefix) = 1 And portion.String <> "" Then If InStr(portion.HyperLinkURL, endnoteInBodyAnchorSuffix) > 0 Then isCustomEndnote = true Exit Function EndIf EndIf isCustomEndnote = false End Function