[Solved] Copy text fields content as strings

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

[Solved] Copy text fields content as strings

Post by Tommy »

hi there,
I have this test .odt file which has 3 text fields with text and numbers inside (it's a medical report template which patient name, date of birth and exam date)

I need a macro to extract the text in those fields as 3 separate strings.

basically in the mockup file I need to extract the string: DONALD DUCK from first field, 10-11-1955 from the second and 27-06-14 from the third one.

is there any basic code function that allows me to "read" a text field and copy it as a string?
Attachments
test_file.odt
(18.38 KiB) Downloaded 261 times
Last edited by Tommy on Sat Jun 28, 2014 4:51 pm, edited 1 time in total.
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
RoryOF
Moderator
Posts: 35203
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [REQ] copy text fields content as strings

Post by RoryOF »

What is to happen to the extracted text? Is it to be placed in separate files, or reused within the test file? How?
Apache OpenOffice 4.1.16 on Xubuntu 24.04.4 LTS
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [REQ] copy text fields content as strings

Post by Tommy »

the extracted string will reused in another macro which compares the correspondence between file name and text fields data, anyway I won't go in further details since I already know how to do this second part and I do not need help about it.

the only thing I miss is how to extract those simple strings from those text fields in that file
once I have those strings I can do all the other stuff.

the macro code I need should identify the 3 text fields inside the .odt file and then read and copy the text of each one.
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
RoryOF
Moderator
Posts: 35203
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [REQ] copy text fields content as strings

Post by RoryOF »

I found this at
https://wiki.openoffice.org/wiki/Docume ... ith_fields
Converting fields into text

Writer does not provide any easy way to convert field contents into text. To do this, you need to copy the field contents and paste them back as unformatted text. This is not a very good solution if you have hundreds of fields that you want to change, but you could use a macro to automate the process.

A suggested macro is provided here: http://www.oooforum.org/forum/viewtopic.phtml?t=115398
This macro may be echoed at
viewtopic.php?f=7&t=65817#p292326
but I haven't read it to verify that.
 Edit: The above copy of the macro on forum.openoffice.org has been tweaked. For completeness, I list here the original code from oooforum, posting 115398

Code: Select all

Sub FieldsToText 'Writer
Dim oDoc,oFields,oEnum,ThisField
oDoc = ThisComponent
oFields = oDoc.getTextFields
oEnum = oFields.createEnumeration
While oEnum.HasMoreElements
ThisField = oEnum.nextElement
ThisField.Anchor.String = ThisField.Anchor.String
Wend 
End Sub
 
Apache OpenOffice 4.1.16 on Xubuntu 24.04.4 LTS
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [REQ] copy text fields content as strings

Post by Tommy »

thanks for your help but it's not exactly what I'm looking for.

I do not need to "convert" the text field into plain text, like that macro does

in the test file I have 3 text field (those written in bold light blue) and if you double click on them you will see that each one has a name and a value

what I need is a macro that "reads" that value and copies it as as string.

in the test file the 1st field is "fldNome" and it's value is "DONALD DUCK",
the 2nd is "fdlNascita" and has a "10-11-1955" value,
the 3rd is "fldEsame" and has a "27-06-2014" value.

all values are in simple text format.

the macro I'm looking for should read those values and extract them as 3 separated text strings.
the output should be: "DONALD DUCK", "10-11-1955" and "27-06-2014"

I will later assemble those strings into a single string which will be "DONALD DUCK_101155_270614" since I already know how to manipulate text strings
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
RPG
Volunteer
Posts: 2261
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [REQ] copy text fields content as strings

Post by RPG »

Hello

I have no solution but maybe this can help you
viewtopic.php?f=20&t=2786&hilit=userfields

Pitonyak did placed there a macro. With the knowledge of that macro and MRI i can see the fields. I have not test it with a macro.

Romke
LibreOffice 24.8.5.2 on openSUSE Leap 15.6
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [REQ] copy text fields content as strings

Post by Villeroy »

A reminder of the character named "Tommy": Stop OpenOffice Renaissance project (petition) Today he marks his personal wishes as a requirement:

Code: Select all

[REQ] copy text fields content as strings
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [REQ] copy text fields content as strings

Post by Tommy »

REQ = request

get a life, Villeroy.
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
MTP
Volunteer
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: [REQ] copy text fields content as strings

Post by MTP »

Does the [Thisfield.Anchor.String] not assign the textfield content to your variable? You wouldn't assign it to itself, you'd assign it to the variable you wanted to have the text. That's how I interpreted the macro Rory posted.
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
User avatar
karolus
Volunteer
Posts: 1242
Joined: Sat Jul 02, 2011 9:47 am

Re: [REQ] copy text fields content as strings

Post by karolus »

Hallo
thanks for your help but it's not exactly what I'm looking for....
so modify it to your needs, or hire profesional Programmers to spoonfeed yourself....

bye .....
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [REQ] copy text fields content as strings

Post by Tommy »

MTP wrote:Does the [Thisfield.Anchor.String] not assign the textfield content to your variable? You wouldn't assign it to itself, you'd assign it to the variable you wanted to have the text. That's how I interpreted the macro Rory posted.
hi MTP,
the macro Rory posted converts the text field into plain text so it does a completely different thing. try it on my test file to see what happens.

I need a macro to just "read" the text value which is inside the text field and to save it as a string to be processed in other macros I already know how to use

see my screenshot. I should figure how which code should I use to extract the DONALD DUCK value from the fldNome field.


@karolus
your comment is offensive and useless like the one from Villeroy.
get a life, too.
Attachments
screenshot.png
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
MTP
Volunteer
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: [REQ] copy text fields content as strings

Post by MTP »

Tommy wrote:...try it on my test file to see what happens.
Well, I tried it (see attached). I had it print the stored variable to check, and it works like I guessed it did.
Attachments
test_file.odt
(13.28 KiB) Downloaded 209 times
Last edited by MTP on Sat Jun 28, 2014 5:50 pm, edited 1 time in total.
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [REQ] copy text fields content as strings

Post by Tommy »

@MTP
thank you very much. your edited version of Rory's macro does exactly what I was looking for.

here's the correct working code

Code: Select all

Sub ReadTextFields
	Dim oFields : oFields = ThisComponent.getTextFields
	Dim oEnum
	Dim ThisField
	Dim s As String
	
	oEnum = oFields.createEnumeration
	While oEnum.HasMoreElements
		ThisField = oEnum.nextElement
		s = ThisField.Anchor.String
		Print s
	Wend
End Sub
many thanks to you, to RoryOF and RPG who spent time trying to help and give good tips.
:super:
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
karolus
Volunteer
Posts: 1242
Joined: Sat Jul 02, 2011 9:47 am

Re: [REQ] copy text fields content as strings

Post by karolus »

Hallo
RoryOF wrote:
 Edit: The above copy of the macro on forum.openoffice.org has been tweaked. For completeness, I list here the original code from oooforum, posting 115398

Code: Select all

Sub FieldsToText 'Writer
Dim oDoc,oFields,oEnum,ThisField
oDoc = ThisComponent
oFields = oDoc.getTextFields
oEnum = oFields.createEnumeration
While oEnum.HasMoreElements
ThisField = oEnum.nextElement
ThisField.Anchor.String = ThisField.Anchor.String
Wend 
End Sub
 
All you had to do was

Code: Select all

ThisField.Anchor.String = ThisField.Anchor.String
replacing with

Code: Select all

print ThisField.Anchor.String
Lazy bum !!
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] copy text fields content as strings

Post by Tommy »

I'm a newbie about coding macros and that code tweak that looked so simple to an expert like you, was not immediate to me.

I see you prefer spending your time insulting people rather than giving help... shame on you.
this forum needs more people like Rory, RPG and MTF.
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
Post Reply