OOo API and text fields (VBScript)

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
RVAma
Posts: 2
Joined: Tue Jan 12, 2016 4:58 pm

OOo API and text fields (VBScript)

Post by RVAma »

Hey folks,

I can't figure out how to access text fields within an existing .odt file that I have created previously.
Im using VBScript and I HAVE to use it, other languages are no option. This script is supposed to be integrated as a module withini another application later.

this is what I got so far:

Code: Select all

    Set objDesktop = objServiceManager.CreateInstance("com.sun.star.frame.Desktop")

    Dim args(0)      : Set args(0) = OO_setPropVal("Hidden", true)
    Dim objDoc      : Set objDoc = objDesktop.loadComponentFromURL("file:///C:/Users/RVama/Desktop/Test_1.odt", "_blank", 0, args)
    Dim textFields  : Set textFields = objDoc.getTextFields()
a call to

Code: Select all

MsgBox textFields.getElementType()
results in "com.sun.star.text.XDependentTextField"
If I get this right, this is the type of the elements WITHIN textFields, not the type of textFields itself, right? 'textFields' is supposed to be a XEnumerationAccess object according to the OOo API documentation (http://www.openoffice.org/api/docs/comm ... plier.html - method "getTextFields").

I read this guide: https://wiki.openoffice.org/wiki/Docume ... ext_Fields
but I still don't know how to access the individual text fields. It can't be that hard, but I don't get it :/

Thank you in advance.
3.3.0 (OOO330m20) Windows 7 Pro SP1
User avatar
MTP
Volunteer
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: OOo API and text fields (VBScript)

Post by MTP »

Your text fields might be form controls. In that case you would need something like

Code: Select all

Dim form : Set form = objDoc.drawpage.forms.getByIndex(0)
Dim textField0 : Set textField0 = form.getByIndex(0)
REM Or: 
Set textField0 = form.getByName("YourFieldName")
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
FJCC
Moderator
Posts: 9543
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: OOo API and text fields (VBScript)

Post by FJCC »

TO access the text fields from the enumeration, you can use something like this (in OpenOffice Basic)

Code: Select all

oTextFields = ThisComponent.getTextFields()
Enum = oTextFields.createEnumeration()
While Enum.hasMoreElements()
	oField = Enum.nextElement()
	print oField.ImplementationName
Wend
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: OOo API and text fields (VBScript)

Post by Villeroy »

Stop hacking an application you don't understand. If you do not use MRI, XRay or some other object inspector, you end up in the middle of nowhere. Before all the methods and properties exposed by your preferred object inspector do make any sense, you need to gain full overview over each and every aspect of a Writer document. Paragraphs, cursors, breaks, styles, hard formatting attributes, frames, field masters, text fields, draw page, shapes, forms, form controls to mention only a few.
I can define all kinds of "text boxes" or "text fields" (form fields and text fields, shapes and frames), fill them with some text content and then find the object with its text content in MRI (which also generates a macro snippet in 4 programming langauges on the fly).
[Tutorial] Introduction into object inspection with MRI
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
RVAma
Posts: 2
Joined: Tue Jan 12, 2016 4:58 pm

Re: OOo API and text fields (VBScript)

Post by RVAma »

Villeroy wrote:Stop hacking an application you don't understand. If you do not use MRI, XRay or some other object inspector, you end up in the middle of nowhere.
Absolutely agree with that. I'm new to VBScript AND the OOo API. Without a good debugger or inspection tool, it makes things harder to understand. Documentation of the API is ok, could be better though. I don't like the fact that when I check a service or interface in the documentation, I don't get an overview of ALL methods and properties available, I have to check all included services and exported interfaces manually. Inherited methods/properties aren't included as well. It's hard to get an overview sometimes.
I downloaded MS Visual Studio Community 2015, which is free, and I use the command "cscript.exe //x <script path>" to debug with it. It does help, but I can't inspect objects with it, which is what I want to do most of the time, so it's not extremely useful.
Villeroy wrote:Before all the methods and properties exposed by your preferred object inspector do make any sense, you need to gain full overview over each and every aspect of a Writer document. Paragraphs, cursors, breaks, styles, hard formatting attributes, frames, field masters, text fields, draw page, shapes, forms, form controls to mention only a few.
That would be the better approach, but unfortunately I don't have the time for that/I'm not getting paid for that. This is a company project I'm working on, and my goal is to create serial/form letters (or mail merge or whatever you call them in english). So digging deep into OpenOffice Writer is a bit overkill and too time consuming, especially since this is a one time thing and there won't be any more OOo projects in the forseeable future.
Villeroy wrote:I can define all kinds of "text boxes" or "text fields" (form fields and text fields, shapes and frames), fill them with some text content and then find the object with its text content in MRI (which also generates a macro snippet in 4 programming langauges on the fly).
[Tutorial] Introduction into object inspection with MRI
That looks very helpful, will check it out, thank you!

I will also try the other suggestions and see how far that will get me, really appreciate the answers, thank you guys!
3.3.0 (OOO330m20) Windows 7 Pro SP1
Post Reply