Set LibreOffice (especially Writer) to use the first line of my document as the file name when saving it
Code: Select all
Sub FirstLineFileName_Writer
On Error goto EH
oDoc = ThisComponent
oVC = oDoc.CurrentController.getViewCursor
Mark = oDoc.Text.CreateTextCursorByRange(oVC) 'mark position of view cursor.
oTC = oDoc.Text.CreateTextCursor 'created at the beginning of doc.
While oTC.isEndOfParagraph 'skip empty paragraphs.
oTC.gotoNextParagraph(false)
Wend
oVC.gotoRange(oTC,false) 'a text cursor can't go to the end of a line
oVC.gotoEndOfLine(true) 'so we have to use the view cursor.
filename = Trim(oVC.String)
' Debug: Print the filename to check for hidden characters
Print "Filename: '" & filename & "'"
' Set the document title to the first line
oDoc.Title = filename
' Open the Save As dialog (filename will be pre-filled)
oDoc.storeToURL("", Array()) ' This will open the Save As dialog
oVC.gotoRange(Mark,false) 'return view cursor to original position.
oDoc.Modified = false 'avoid Save being called if doc closed without further edits.
End 'end normal execution.
EH: 'error handler.
MsgBox "You may have illegal file name characters in the first line." & Chr(13) & Chr(13) & filename,,"AN ERROR OCCURRED"
End Sub