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 section is not for asking questions about writing your own macros.
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.
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?
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.