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!

 

Tools Macros Organize Macros LibreOffice Basic

Opening the macro menu

New to add a new macro.

Adding a new macro

Run Macro then choose the macro manually from the menu. But that’s not too handy, is it?

Customizing the key combination

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.

CTRL + S and see what happens!



Muaad Elsharif

A blogger from Tripoli Libya. مدون من طرابلس ليبيا