Page 1 of 1

[Solved] Add AutoTextEntry with Field

Posted: Wed Apr 10, 2019 5:31 pm
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

Re: Add AutoTextEntry with Field

Posted: Wed Apr 10, 2019 9:00 pm
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.

Re: Add AutoTextEntry with Field

Posted: Wed Apr 10, 2019 9:43 pm
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

Re: Add AutoTextEntry with Field

Posted: Wed Apr 10, 2019 10:04 pm
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


Re: Add AutoTextEntry with Field

Posted: Fri Apr 12, 2019 6:23 pm
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

Re: Add AutoTextEntry with Field

Posted: Fri Apr 12, 2019 10:40 pm
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)


Re: Add AutoTextEntry with Field

Posted: Sun Apr 14, 2019 11:44 am
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

Re: [Solved] Add AutoTextEntry with Field

Posted: Wed Apr 24, 2019 5:47 pm
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