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

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.

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

This fixed my problem. Thank you very much.