Page 1 of 1

OOo API and text fields (VBScript)

Posted: Tue Jan 12, 2016 5:26 pm
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.

Re: OOo API and text fields (VBScript)

Posted: Tue Jan 12, 2016 8:15 pm
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")

Re: OOo API and text fields (VBScript)

Posted: Tue Jan 12, 2016 9:10 pm
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

Re: OOo API and text fields (VBScript)

Posted: Tue Jan 12, 2016 9:38 pm
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

Re: OOo API and text fields (VBScript)

Posted: Wed Jan 13, 2016 11:05 am
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!