Macro with ReferenceMarks does not work in footnotes

Keyboard macros or custom scripts

Macro with ReferenceMarks does not work in footnotes

Postby teertinker » Wed Oct 14, 2020 4:05 pm

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?

Code: Select all   Expand viewCollapse view
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
teertinker
 
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby JeJe » Wed Oct 14, 2020 9:58 pm

Move the dispose line to after that one.

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.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1531
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby teertinker » Thu Oct 15, 2020 11:20 am

Dear Jeje,
thanks a lot! I will try your suggestions today and let you know about the results.
Openoffice 3.1
teertinker
 
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby teertinker » Thu Oct 15, 2020 1:14 pm

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/libreoffice-macro-access-footnote-text and tried
Code: Select all   Expand viewCollapse view
  oText = ThisComponent.gettext()

but this didn't change anything.

Code: Select all   Expand viewCollapse view
'''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
Openoffice 3.1
teertinker
 
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby JeJe » Thu Oct 15, 2020 1:45 pm

Try

Code: Select all   Expand viewCollapse view
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)

Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1531
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby teertinker » Thu Oct 15, 2020 1:52 pm

Yuppie! You saved my day! Wonderful it works - even though I don't understand the logic behind it.
Openoffice 3.1
teertinker
 
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby JeJe » Thu Oct 15, 2020 1:59 pm

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.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1531
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby teertinker » Thu Oct 15, 2020 10:17 pm

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.
Openoffice 3.1
teertinker
 
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby JeJe » Thu Oct 15, 2020 10:34 pm

Instead of

oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor()
try

oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor().getstart

Or instead of insert string you can do


Code: Select all   Expand viewCollapse view
    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

Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1531
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby teertinker » Fri Oct 16, 2020 1:48 pm

I had also the idea to use getstart. Unfortunately it does not work. The string will still become part of the field and thus gets disposed.
Openoffice 3.1
teertinker
 
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby JeJe » Fri Oct 16, 2020 2:16 pm

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.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1531
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby teertinker » Fri Oct 16, 2020 2:30 pm

The document should include the macro.

https://www.dropbox.com/scl/fi/227ftkom ... syqoj46gof
Openoffice 3.1
teertinker
 
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby JeJe » Fri Oct 16, 2020 3:01 pm

Try:

Code: Select all   Expand viewCollapse view
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


Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1531
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby teertinker » Sat Oct 17, 2020 10:51 am

Wonderful! It works like a charm now. Thank you so much. I hope its ok to include your suggestion into my extension (https://github.com/teertinker/JabRef_Li ... _Converter).
Openoffice 3.1
teertinker
 
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Postby JeJe » Sat Oct 17, 2020 1:18 pm

I hope its ok to include your suggestion into my extension (https://github.com/teertinker/JabRef_Li ... _Converter).


Of course - however you like.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1531
Joined: Wed Mar 09, 2016 2:40 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 4 guests