Page 1 of 1

[Writer] Save with first line as file name.

Posted: Tue Jun 23, 2009 4:48 pm
by JohnV
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

Re: [Writer] Save with first line as file name.

Posted: Wed Sep 09, 2009 11:52 am
by Foxfan100
I like this very much as it's one of the few things in MS Word that's useful but which is missing in OO.

If I assign it to "Save As" and "Save", how do I avoid OO apparently wanting to save the document with the OO document default name of "Untitled1.odt" before it eventually saves it with first line as name?

Re: [Writer] Save with first line as file name.

Posted: Wed Sep 09, 2009 11:49 pm
by JohnV
Don't assign the macro to the Save or Save As events. Assign it to a keyboard combo so those events are never called.

I made a couple of changes to the original code so you might want to copy it again. The first will add the ".odt" extension to the file. The second sets "oDoc.Modified = False" which will avoid the Save dialog from popping up if you close the document without further edits.

Re: [Writer] Save with first line as file name.

Posted: Fri Sep 11, 2009 1:31 pm
by Hagar Delest
For the record, you can add your votes here: Issue 22561 - Set heading of document as file name and title.

Re: [Writer] Save with first line as file name.

Posted: Mon Nov 30, 2009 9:02 pm
by JimDodds
This macro looks great but I'm a real newbie - duh! What do I do with it?

Thank you!

Re: [Writer] Save with first line as file name.

Posted: Mon Nov 30, 2009 9:06 pm
by JohnV