[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.

[Writer] Save with first line as file name.

Postby JohnV » Tue Jun 23, 2009 4:48 pm

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   Expand viewCollapse view
    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.
JohnV
Volunteer
 
Posts: 1584
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

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

Postby Foxfan100 » Wed Sep 09, 2009 11:52 am

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?
Foxfan100
 
Posts: 23
Joined: Mon Sep 01, 2008 6:29 pm

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

Postby JohnV » Wed Sep 09, 2009 11:49 pm

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

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

Postby Foxfan100 » Thu Sep 10, 2009 11:50 am

As they say..... "Doh!" I should have realised that.

It works exactly as it should with a keyboard combination.

Full marks for a really useful macro.

Cheers,
Peter
OOo 2.3.X on Ms Windows XP + Ubuntu Happy Heron
Foxfan100
 
Posts: 23
Joined: Mon Sep 01, 2008 6:29 pm

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

Postby Hagar Delest » Fri Sep 11, 2009 1:31 pm

For the record, you can add your votes here: Issue 22561 - Set heading of document as file name and title.
AOO 4.1.4 on Xubuntu 17.10, (Artful Aardvark) and 4.1.3 on Windows 7 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 27655
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

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

Postby JimDodds » Mon Nov 30, 2009 9:02 pm

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
JimDodds
 
Posts: 1
Joined: Mon Nov 30, 2009 8:56 pm

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

Postby JohnV » Mon Nov 30, 2009 9:06 pm

JohnV
Volunteer
 
Posts: 1584
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

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

Postby gopalakrishnanms » Thu Jul 07, 2011 1:58 pm

I am making a medical record [patient discharge summary] template where users can enter various fields like name, age hosptal number etc. Could you modify this macro of yours to automatically create file name to get data entered in fields so that saved files are uniform and easily searcheable. Say, the output might look like James_25_F341534_07/07/2011.odt
I am not familiar with macros or codes ( I am a neurosurgeon) but I can try modifying your code if you could give a few suggestion on how to retreive data from fields. Thank you!
OpenOffice 3.2.0 on Windows Vista
gopalakrishnanms
 
Posts: 2
Joined: Thu Jul 07, 2011 12:55 pm

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

Postby JohnV » Thu Jul 07, 2011 4:52 pm

This macro is in the file attached.

Code: Select all   Expand viewCollapse view
Sub SaveWithFieldNames
On error goto EH
oDoc = ThisComponent
oTFS = oDoc.getTextFields
enum = oTFS.createEnumeration
While enum.hasMoreElements
oTF = enum.nextElement
Select Case oTF.Content
Case "Name" : N = oTF.Anchor.String
Case "Age" : A = oTF.Anchor.String
Case "Hosp" : H = oTF.Anchor.String
Case "Date" : D = oTF.Anchor.String
End Select
Wend
U = "_"
aray = Split(D,"/") 'convert slashes to dashes.
D = Join(aray,"-")
filename = N & U & A & U & H & U & D & ".odt"
url = ConvertToURL("C:\users\cat\documents\" & filename) 'Insert Your Desired Directory Path.
oDoc.StoreAsURL(url,Array())
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 character." & Chr(13)_
& Chr(13) & filename,,"AN ERROR OCCURRED"
End Sub
Attachments
FieldsAsFileName.ott
(11.1 KiB) Downloaded 484 times
JohnV
Volunteer
 
Posts: 1584
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

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

Postby ozirisra » Mon Mar 11, 2013 11:35 am

Hi,

This works really great and helped A LOT !!! I'm mainly dealing with VBA, but have to do it with OO this time, so I'm quite a beginner in this.
I'm trying to modify the code to rename ALL files from a certain folder. My problem is that I cannot assign the NextFile that I get to work with the ThisComponent statement.
So, how can I make the NextFile to be the active file and work with ITS first line?

Here's the code that I have so far:

Code: Select all   Expand viewCollapse view
Sub FirstLineFileName_Writer
Dim NextFile As String
NextFile = Dir("C:\...\", 0)

     While NextFile  <> ""
        oDoc = ThisComponent
        oVC = oDoc.CurrentController.getViewCursor
        Mark = oDoc.Text.CreateTextCursorByRange(oVC)
        oTC = oDoc.Text.CreateTextCursor
        While oTC.isEndOfParagraph
        oTC.gotoNextParagraph(false)
        Wend
        oVC.gotoRange(oTC,false)
        oVC.gotoEndOfLine(true) 
        filename = oVC.String
        url = ConvertToURL("C:\" & filename & ".odt")
        oDoc.StoreAsURL(url,Array())
        oVC.gotoRange(Mark,false)
        oDoc.Modified = false
         
NextFile = Dir
Wend

   End Sub


Thanks for any help
LibreOffice 3.6.4.3 on windows 7
ozirisra
 
Posts: 1
Joined: Mon Mar 11, 2013 11:18 am

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

Postby FJCC » Tue Mar 12, 2013 2:16 am

The problem is that you do not open the document whose name is returned by the Dir() function. Here is some code that opens a document and then closes it. I think you can use it to modify the appropriate parts of your code.
Code: Select all   Expand viewCollapse view
DirString = "C:\users\public\databases\"
NextFile = DirString & Dir(DirString,0)
NextURL = convertToURL(NextFile)
oDoc = StarDesktop.loadComponentFromURL(NextURL, "_blank", 0, Array())
Print oDoc.URL
oDoc.Close(False)
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 6160
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

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

Postby lvosheva » Fri Aug 22, 2014 1:42 am

Hi guys!
I need a macro to do a dynamic save that takes the first line of each document and uses it to save it. I'm converting from a template to regular .docx

Please - I've been at it for several hours now and am stuck on the activedocument.save command - it doesn't want to work on me. I've tried two different approaches. The main problem is the dynamic save part (where it saves each document with its' unique name). I can get the name either from the field code that is in every single doc (there are about 200+ docs) or it can grab it from the header on pages 2 through the last page.

Please please help.
OpenOffice 3.1 on Windows 7
lvosheva
 
Posts: 1
Joined: Fri Aug 22, 2014 1:37 am

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

Postby glorybackstay424 » Tue Oct 04, 2016 11:53 am

JohnV wrote: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   Expand viewCollapse view
    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

This is what I was searching for. Thanks
glorybackstay424
 
Posts: 1
Joined: Wed Sep 28, 2016 1:02 pm


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 2 guests