[Solved] Add AutoTextEntry with Field

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Diez
Posts: 7
Joined: Tue Mar 05, 2019 10:37 pm

[Solved] Add AutoTextEntry with Field

Post by Diez »

Hello all.

I'd like to add an AutoTextEntry into a AutoTextContainer, but containing a Field, a DateTimeField. I know how to add an AutoTextContainer and I know how to add a plain text AutoTextEntry, but I couldn't to do the same with an AutoTextEntry containing a DateTimeField at the end of the Entry. I'd like to use Basic.

I'm trying with insertTextContent, but the added entry is in plain text.

Regards and thanks in advanced
Last edited by Hagar Delest on Sun Apr 14, 2019 11:57 am, edited 1 time in total.
Reason: tagged solved
LibreOffice 6 on Debian Testing
User avatar
Villeroy
Volunteer
Posts: 31265
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Add AutoTextEntry with Field

Post by Villeroy »

AutoText is one of those features which allow you to do amazing things without Basic. Using AutoText you don't need Basic and if you prefer Basic (shudder) then you don't need AutoText.
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
Diez
Posts: 7
Joined: Tue Mar 05, 2019 10:37 pm

Re: Add AutoTextEntry with Field

Post by Diez »

Hello

The idea is register a bunch of AutoTextEntry all related with a kind of documents, some of it are only plain text, but some of it have Fields. The way is an option menu in a template in order to perform the register of the entries.

The plan is use some templates by many users, but I wouldn't like to add entries in all computers in person, I prefer an menu option and one or more macros, or perhaps some kind of installation

Regards
LibreOffice 6 on Debian Testing
JeJe
Volunteer
Posts: 2756
Joined: Wed Mar 09, 2016 2:40 pm

Re: Add AutoTextEntry with Field

Post by JeJe »

Worked with Macro recorder...
Select what you want as an autotext, then modify this for the correct names...


Code: Select all

sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args3(2) as new com.sun.star.beans.PropertyValue
args3(0).Name = "Group"
args3(0).Value = "mytexts*1"
args3(1).Name = "Name"
args3(1).Value = "pp"
args3(2).Name = "ShortName"
args3(2).Value = "ppauto"

dispatcher.executeDispatch(document, ".uno:NewGlossary", "", 0, args3())
end sub

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Diez
Posts: 7
Joined: Tue Mar 05, 2019 10:37 pm

Re: Add AutoTextEntry with Field

Post by Diez »

Hello.

Now I'm testing this code:

Code: Select all

Sub AutoTextManagement
	Dim oContainer as Object
	Dim oGroup as Object
	Dim oEntry as Object
	Dim oString as Object
	Dim oField as Object
	
	Dim s as String
	Dim n as Integer
	Dim b as Boolean
	
	REM List all groups
	s = ""
	oContainer = CreateUnoService("com.sun.star.text.AutoTextContainer")
	For n = 0 To oContainer.Count - 1
		s = s & oContainer.ElementNames(n) & CHR(13)
	Next n
	MsgBox s
	
	REM Search a group by name
	b = oContainer.hasByName("NonExistent")
	If Not b Then
		MsgBox "NonExistent not found. Creating..."
		REM Creating if doesn't exist
		oContainer.insertNewByName("NonExistent*1")
	End If
	
	REM Now "NonExistent" exists
	oGroup = oContainer.getByName("NonExistent")
	
	REM Checking if exists XXL AutoTextEntry
	b = oGroup.hasByName("XXL")
	If Not b Then
		MsgBox "XXL not found. Creating..."
		oString = ThisComponent.Text
		oString.insertString(oString.getEnd(),"Text of the entry: ",False)
		oField = ThisComponent.createInstance("com.sun.star.text.TextField.DateTime")
		oField.IsFixed = True
		oField.IsDate = False
		oString.insertTextContent(oString.getEnd(),oField,False)
		oEntry = oGroup.insertNewByName("XXL","Title of Entry",oString)
	End If
End Sub
but the entry added is in plain text. In the document you can see text and field.

Regards
LibreOffice 6 on Debian Testing
JeJe
Volunteer
Posts: 2756
Joined: Wed Mar 09, 2016 2:40 pm

Re: Add AutoTextEntry with Field

Post by JeJe »

try changing the last line to this:

Code: Select all

cur = thiscomponent.Text.createTextCursorByRange(thiscomponent.text)

      oEntry = oGroup.insertNewByName("XXL8","Title of Entry",cur)

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Diez
Posts: 7
Joined: Tue Mar 05, 2019 10:37 pm

Re: Add AutoTextEntry with Field

Post by Diez »

Hello.

Finally I got it using TextCursor, below the code

Code: Select all

Sub AutoTextManagement2
	Dim oContainer as Object
	Dim oGroup as Object
	Dim oEntry as Object
	Dim oString as Object
	Dim oContent as Object
	Dim oField as Object
	
	Dim s as String
	Dim n as Integer
	Dim b as Boolean
	
	REM List all groups
	s = ""
	oContainer = CreateUnoService("com.sun.star.text.AutoTextContainer")
	For n = 0 To oContainer.Count - 1
		s = s & oContainer.ElementNames(n) & CHR(13)
	Next n
	MsgBox s
	
	REM Search a group by name
	b = oContainer.hasByName("NonExistent")
	If Not b Then
		MsgBox "NonExistent not found. Creating..."
		REM Creating if doesn't exist
		oContainer.insertNewByName("NonExistent*1")
	End If
	
	REM Now "NonExistent" exists
	oGroup = oContainer.getByName("NonExistent")
	
	REM Checking if exists XXL AutoTextEntry
	b = oGroup.hasByName("XXL")
	If Not b Then
		MsgBox "XXL not found. Creating..."
		oField = ThisComponent.createInstance("com.sun.star.text.TextField.DateTime")
		oField.IsFixed = True
		oField.IsDate = False	
		oString = ThisComponent.Text.createTextCursorByRange(ThisComponent.Text)
		oString.String = "Text of entry: "
		oContent = oString.getText
		oContent.insertTextContent(oContent.getEnd(),oField,False)
		oEntry = oGroup.insertNewByName("XXL","Title of Entry",oString)
		oString.String = ""
	End If
End Sub
I need to think how to integrate because with this code I need a document to write the AutoTextEntry on to it before register, the idea is use the code from a template so in this way, I think I have to create an empty document before run the code, in order to write the entries, and preserve the template.

Regards
LibreOffice 6 on Debian Testing
Diez
Posts: 7
Joined: Tue Mar 05, 2019 10:37 pm

Re: [Solved] Add AutoTextEntry with Field

Post by Diez »

Hello

Below a final version using document to write AutoText and after closing and releasing it. Setting NumberFormat for DateTime Field is not working, I don't know why. I'm looking for the way to set the format.

Code: Select all

Sub AutoTextManagementWithDocument
	Dim oContainer as Object
	Dim oGroup as Object
	Dim oEntry as Object
	Dim oString as Object
	Dim oContent as Object
	Dim oField as Object
	Dim oDoc as Object
	
	Dim s as String
	Dim n as Integer
	Dim b as Boolean
	
	REM List all groups
	s = ""
	oContainer = CreateUnoService("com.sun.star.text.AutoTextContainer")
	For n = 0 To oContainer.Count - 1
		s = s & oContainer.ElementNames(n) & CHR(13)
	Next n
	MsgBox s
	
	REM Search a group by name
	b = oContainer.hasByName("NonExistent")
	If Not b Then
		MsgBox "NonExistent not found. Creating..."
		REM Creating if doesn't exist
		oContainer.insertNewByName("NonExistent*1")
	End If
	
	REM Now "NonExistent" exists
	oGroup = oContainer.getByName("NonExistent")
	
	REM Checking if exists XXL AutoTextEntry
	b = oGroup.hasByName("XXL")
	If Not b Then
		oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array())
		MsgBox "XXL not found. Creating..."
		oField = ThisComponent.createInstance("com.sun.star.text.TextField.DateTime")
		oField.IsFixed = True
		oField.IsDate = False	
		oField.NumberFormat = 20040
		oString = oDoc.Text.createTextCursorByRange(oDoc.Text)
		oString.String = "Text of entry: "
		oContent = oString.getText
		oContent.insertTextContent(oContent.getEnd(),oField,False)
		oEntry = oGroup.insertNewByName("XXL","Title of Entry",oString)
		oString.String = ""
		oDoc.dispose()
	End If
End Sub
Regards
LibreOffice 6 on Debian Testing
Post Reply