Dear Community,
I have build a small macro that tries to convert referencemarks into a text code. The sub routine works quite ok at the moment. My Problem is, that all reference marks in footnotes won't be converted. Instead OpenOffice throws an exception at the "oText.insertString(oViewCursor, "\cite{" +temp + "}", false)" line. Has anyone a tip for me?
Sub FromReferenceToText
dim temp as string
Dim Pos As Long
Dim oText As Object
Dim oViewCursor As Object
Dim i As Integer, oRefmarks, oRefmarkNames()
oText = ThisComponent.Text
oRefmarks = ThisComponent.getReferenceMarks()
oRefmarkNames = oRefmarks.getElementNames()
For i = 0 To Ubound( oRefmarkNames )
If instr(oRefmarkNames(i), "JR_cite") Then
oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor()
oRefmarks.getByName( oRefmarkNames( i ) ).dispose()
temp=oRefmarkNames(i)
if left(temp,7)="JR_cite" then
temp = mid(temp,8)
end if
oText.insertString(oViewCursor, "\cite{" +temp + "}", false)
End if
Next
End Sub
Edit: if that doesn't work run the for-next a second time and do the dispose then (not in the first for-next). Or try reversing the order of the for-next and go backwards from the highest to the lowest index, step -1.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Dear Jeje,
I followed your suggestion and did a second for-next loop. This did not exactly solve my problem, but directs me closer to the error. If I run only the second loop (dispose) every reference mark gets deleted properly (including footnotes). The error is already thrown in the first loop when I try to insert the string into the footnote. I found another discussion about accessing footnotes in https://superuser.com/questions/1289749 ... tnote-text and tried
'''first loop
Dim a as integer
dim temp as string
Dim Pos As Long
Dim oText As Object
Dim oViewCursor As Object
Dim i As Integer, oRefmarks, oRefmarkNames()
oViewCursor = ThisComponent.GetCurrentController.ViewCursor
oText = ThisComponent.text
For i = 0 To Ubound( oRefmarkNames )
If instr(oRefmarkNames(i), "JR_cite") Then
oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor()
temp=oRefmarkNames(i)
if left(temp,7)="JR_cite" then
temp = mid(temp,8)
end if
oText.insertString(oViewCursor, "\cite{" +temp + "}", false)
End if
Next
'''second loop
For i = 0 To Ubound( oRefmarkNames )
If instr(oRefmarkNames(i), "JR_cite") Then
'oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor()
oRefmarks.getByName( oRefmarkNames( i ) ).dispose()
End if
Next
oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor()
temp=oRefmarkNames(i)
if left(temp,7)="JR_cite" then
temp = mid(temp,8)
end if
oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor().getText.insertString(oViewCursor, "\cite{" +temp + "}", false)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Different parts of the document have their own text. The main document has its text. But each text frame also has its own separate text. A table consists of cells and each cell has its own text. You have to use the correct text.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
The code still has a small "hickup". I moved the dispose command below the insert command as suggested in your first comment. That works well for all kinds of ReferenceMarks.
However, if the referencemark is at the beginning of the paragraph, the inserted text becomes part of the referencemark and will be disposed as well. Do you have an idea how I can prevent this behavior?
If I call the dispose command first, there is the problem that reference marks without text create an exception when the insert command is called.
ref = oRefmarkNames(i)
If instr(ref, "JR_cite") Then
oViewCursor= oRefmarks.getByName( ref ).getAnchor().getstart
temp=ref
if left(temp,7)="JR_cite" then
temp = mid(temp,8)
end if
oViewCursor.string = "\cite{" +temp + "}"
oRefmarks.getByName( ref ).dispose()
End if
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Worked in the simple one instance test I created. If you post your document stripped of any sensitive information (and with the code) then people can look at it.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
REM ***** BASIC *****
Sub FromReferenceToText
dim temp as string
Dim oText As Object
Dim i As Integer, oRefmarks, oRefmarkNames()
oRefmarks = ThisComponent.getReferenceMarks()
oRefmarkNames = oRefmarks.getElementNames()
For i = 0 To Ubound( oRefmarkNames )
ref = oRefmarkNames(i)
If instr(ref, "JR_cite") Then
temp=ref
if left(temp,7)="JR_cite" then
temp = mid(temp,8)
end if
anchor = oRefmarks.getByName( ref).getanchor
rr= anchor.gettext.createtextcursorbyrange(anchor)
rr.string="\cite{" +temp + "}"
End if
Next
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)