[Writer] Save with first line as file name.
Posted: Tue Jun 23, 2009 4:48 pm
As indicated, this macro will save a Writer file using the first line of text in the file as the file name.
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 = oVC.String
url = ConvertToURL("C:\" & filename & ".odt")'Insert Your Desired Directory Path.
oDoc.StoreAsURL(url,Array())
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