[Writer] Save with first line as file name.

Creating Extension - Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This forum is not for asking questions about writing your own macros.
Locked
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

[Writer] Save with first line as file name.

Post 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
Last edited by JohnV on Wed Sep 09, 2009 11:53 pm, edited 2 times in total.
Foxfan100
Posts: 23
Joined: Mon Sep 01, 2008 6:29 pm

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

Post 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?
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

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

Post 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.
User avatar
Hagar Delest
Moderator
Posts: 30523
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

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

Post by Hagar Delest »

For the record, you can add your votes here: Issue 22561 - Set heading of document as file name and title.
LibreOffice 7.2.6 on Xubuntu 22.04 and 7.2.6 portable on Windows 10
JimDodds
Posts: 1
Joined: Mon Nov 30, 2009 8:56 pm

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

Post by JimDodds »

This macro looks great but I'm a real newbie - duh! What do I do with it?

Thank you!
OpenOffice 3.1 on Windows 7 Pro
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

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

Post by JohnV »

Locked