Setting the first line as the file name in LibreOffice

LibreOffice Logo
One of the few features I missed moving from Microsoft Office was the auto name upon save, where Word takes the first line of the text and uses it to name the text file. LibreOffice (and OpenOffice before that) don't have this feature! It's a small thing but over time it becomes a growing nuisance!
Luckily there is an easy fix for that! Which enables you to enjoy the luxury of easy naming of files and limit the Untitled flood over time! 
We are going to use a feature known as macros. macros in office are a Visual Basic code that is used to preform a certain task over and over and is triggered by an event. In this post I'll be showing how to create a macro that saves the file using the first line of text when pressing the key combination of CTRL + S (assigning the macro to save or save as will result in two files: untitled and the first line of text!)
So at first, we are going to need our macro. I took this code as is from an OpenOffice form, it works fine without editing:
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("D:\" & 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
This code is used to save the text file by taking the first line of text and setting it as the name. You can edit the directory of the save. Please note that it saves without conformation! Don't worry if you don't see a dialog because that's how this macro is!
 We are going to add this macro to our macros, so we are going to navigate to LibreOffice and go to  

Tools > Macros > Organize Macros > LibreOffice Basic

Opening the macro menu


Click New to add a new macro.


Adding a new macro


In the new dialog paste the macro as is, choose the first line as a name. And close to save.
Now we have our macro, we can run it manually by going to Tools > Macros > Run Macro > then choose the macro manually from the menu. But that's not too handy, is it?
To make the macro actually useful. We need to add it to an event or a key combination to truly unleash it's power!
To do so navigate to Tools > Customize and at the keyboard tab choose the key combination you like. 

Customizing the key combination


Then at the functions choose LibreOffice macros > Standard > FirstLineFileName_Writer and press OK. And make sure that it's applied to LibreOffice and not at Writer from the radio buttons on the left.
That is that!!
To make sure it worked open a blank text document. Write a few words and press CTRL + S and see what happens!

Now you know how to use macros to increase your productivity. Pretty cool wasn't it?
Do you use macros? What are your must-have macros? Care to share?

Comments

  1. Any way to do a macro that opens the regular save as dialog and assign the first line as filename? Be able to choose the directory still useful.

    ReplyDelete

Post a Comment

شاركني برأيك في التعليقات. لا تقرأ وترحل!!