[Writer] Save with file name from fields

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 section is not for asking questions about writing your own macros.
Post Reply
gopalakrishnanms
Posts: 2
Joined: Thu Jul 07, 2011 12:55 pm

[Writer] Save with file name from fields

Post by gopalakrishnanms »

 Edit: Split from [Writer] Save with first line as file name 
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
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 »

This macro is in the file attached.

Code: Select all

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 837 times
Post Reply