Custom field display


I still have a question about fields.
The thing is I have to convert an existing Word macro in VBA into the same thing, written in Basic for LibreOffice.

In the VBA macro, there’s a window with a list and a button, and when you select an element on the list and click on the button, it inserts a pair of fields inside the document. The inserted fields look like this :


These fields are not meant to be replaced by anything, they’re just like containers. And each container will be able to contain specific fields.

To do that, I have another window, containing a list of fields, and when the user clicks on a field and clicks on validate, it will insert the field in the document.

If the cursor is placed outside of the “SECTION” then no field can be added.
And depending on the cursor, certain fields can be added and not others. This means that if the cursor is inside the “COMPOSITION SECTION” like above, then some specific fields will be shown and only these fields will be inserted. I’m not sure if I’m very clear…

I’m explaining all that so that you have a full understanding of what I’m trying to achieve.

Thanks to @KamilLanda I was able to insert the sections inside the document. But what bothers me is the way it’s displayed… If I take the example of “composition” like above, when it’s inserted inside my LibreOffice document, it looks like this :

Champ d'utilisateur SECT_COMPOSITION=  Champ d'utilisateur SECT_END_COMPOSITION=  

I was not able to create custom fields with opening and closing tags, I don’t know why, so I had to make them a little different : the opening field will be normal and the closing one will have the “END” word in it.

But they’re preceeded by the word “Champ d’utilisateur” which means “User field”, and they’re both followed by the equal sign…

So when you look at the document, these fields really look like fields to be replaced, and not section tags…

Do you understand what my issue is ?
Is there a way to do it differently ? Maybe another type of fields that I’m not aware of ?

I can give you the VBA code if needed, it may be clearer for a better undertanding ?

I hope you’ll be able to help me :slight_smile:

I don’t understand well what you need, I absolutely don’t know MSO and VBA. Upload example ODT with description of problem you need solve, it will be better than VBA code that is mostly good for nothing, Libre has really different API.

Sometimes is hard to detect where the cursor is, see Macro to manipulate text within a text box
And it is possible to simulate the key F2 or Enter to get access to some Textbox Macro to set shape in Writer in text edit mode

What I mean is that my macro will insert some fields inside the document, but there will be 2 different types of fields :

  • Sections are fields that will be added in pairs inside the document. There will always be an “opening” and “closing” field. In the Word macro, the inserted fields look like HTML fields, like this : </SECTION MASTER> but it seems that with LO it’s not possible.

Could you just confirm ? I tried to insert a field manually with <> but it didn’t work.

Basically, my problem with LO is that my sections fields don’t look like tags at all and I wanted to know if there was a way to make them look a little better.

I will give you the example of how it looks like in Word:


and inside LO it looks like this :

User field SECT_MASTER= User field SECT_END_MASTER=

As you can see, in the first example, we can easily understand that the fields are like opening and closing tag. And in the 2nd example, it’s not as clear…

So I was wondering if there was a way to make these fields look a little different. For example, is it possible to get rid of the equals sign at the end ? In my case, these fields will never be replaced by any value, so I don’t need this equal sign at all.

And the “User field” is also too much. Ieadlly, I would like to have something like :


It doesn’t really look like opening and closing tags, but it’s much better than what it is now.

Do you understand better now ?

I forgot to talk about the second type of fields :

  • regular fields that will be placed inside the tags that I talked about

I still don’t understand clearly, but something like this?
Macro finds the string and put the Field at the end of found string.
findAndInsert.odt (20.6 kB)

I’m sending you a document, maybe my question will be clearer this time :slight_smile:
I’m sorry for all this mess but it’s a little hard to explain, as I’m trying to convert an existing VBA macro into a LO one…

Sans nom 3.odt (22.3 KB)

I think I found the kind of field that I need to insert into the document.
I was able to insert it manually but I don’t know how to insert this type of field inside the macro.

Maybe you can tell me ?

To insert it manually I git to “Insert / Field / other fields” and here I choose option 2 which is “Display the variable”.
Of course, I have to create it first by choose “Define a variable” (option 1).

So basically what I need to do is find a way to check if the variable exists. I think it can be achieved like this :

if oDoc.getTextFieldMasters.hasByName("") then

Then, in case it doesn’t exist, I have to create it, but I don’t know how…
I tried this :

oUserFieldMaster = oDoc.createInstance("" & sCalculatedFieldName) 

but it didn’t work…

The work with user fields isn’t so easy, but it seems functional.
Based on Andrew Pitonyak Macro Book 4th edition → chapters: 14.12.2-3

Sub insertField
	dim oDoc as object, oFields as object, sName$, oMasterField as object, oField as object, oVCur as object
	sName="my1" 'name of your field
	if oFields.hasByName("" & sName) then 'field exists
		oField=oFields.getByName("" & sName)
		msgbox(sName & " -> " & oField.DependentTextFields(0).Content, 48, "Field exists") 'view value
	else 'create new field
		with oMasterField
		end with	
		with oField
		end with
		oVCur=oDoc.CurrentController.ViewCursor 'visible cursor
		oDoc.Text.insertTextContent(oVCur, oField, False)
	end if
End Sub

I’m sorry but finally I will have to insert another type of fields…
As I told you, I have to convert a VBA macro, so the file initially used is a Microsoft Word document.
I opened it and saved it as a LibreOffice file, and here’s what I got (I will attach a text document in this post) : InfoDocument field

I don’t really know what this field is and how I can insert this kind of field within the macro.

Could you help me with this ?

Thanks for your help.

testWithFields.odt (13.7 KB)

Try this testWithFields-KL1.odt (27.6 kB). It seems it is functional, but I don’t know if 100% → there are notes in document.

1 Like

Thanks a lot KamiLanda !!
This is exactly what I needed to do !