Ask Your Question
0

Add text to a LibreOffice writer text and have the text update in real time

asked 2017-08-30 15:54:33 +0100

yetoo gravatar image

I am trying to make a macro for LibreOffice Writer so that when I press an assigned function key on the keyboard, the macro runs and appends text to my document. This following code snippet is what I have manage to find after scouring the web:

sub file_io_write
 Dim filename As String
 Dim num As Integer
 filename = "/tmp/Untitled.odt"
 num = FreeFile()
 open filename for output as #num 
 write #num, "col1", "col2", "col3"
 write #num, "1", "2", "3"
 write #num, "4", "5", "6"
 close #num
 msgbox "Done"
end sub

The only problem here is that the text doesn't update in real time, I have to reload the document manually in order to view the text. Is there a function that will allow bypassing the required action of manually reloading the document? I'm fine with quick and dirty methods such as simply reloading the document from file. Thank you for your time.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2017-08-30 16:38:09 +0100

librebel gravatar image

Hello @yetoo,

Please try the following:

Sub file_io_write2()
REM Call this method to insert some text into the current Writer document.

    Dim oDoc As Object
    oDoc = ThisComponent        REM ThisComponent always refers to the current document.

    Dim oText As Object
    oText = oDoc.getText()

    REM Insert some Strings at the end.
    oText.insertString( oText.End, "col1" & chr(9) & "col2" & chr(9) & "col3" & chr(10), False )
    oText.insertString( oText.End, "1" & chr(9) & "2" & chr(9) & "3" & chr(10), False )
    oText.insertString( oText.End, "4" & chr(9) & "5" & chr(9) & "6" & chr(10), False )
End Sub
edit flag offensive delete link more

Comments

This fixed my problem. Thank you very much.

yetoo gravatar imageyetoo ( 2017-08-31 08:33:02 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-08-30 15:54:33 +0100

Seen: 117 times

Last updated: Aug 30 '17