PyUNO : Inserting a User field

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

PyUNO : Inserting a User field

et.gaudrain
Dear list,

First I hope I am in the right place... Please forgive me and direct me
to the appropriate mailing-list/forum/whatever if I'm off-topic...

I'm trying to develop an application that would interact with OpenOffice
via PyUNO, and I am not able to create a User field. I started from the
Java example here:
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Fields,
and tried to convert it in Python...

Here is my code:

#-------------------------------------
import uno

localContext = uno.getComponentContext()
resolver =
localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
localContext )
ctx = resolver.resolve(
"uno:pipe,name=OOo_pipe;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager

desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
model   = desktop.getCurrentComponent()

if model.getImplementationName()=='SwXTextDocument':
    xUserField = model.createInstance("com.sun.star.text.TextField.User")
    xMasterPropSet =
model.createInstance("com.sun.star.text.FieldMaster.User")
    xMasterPropSet.setPropertyValue("Name", "UserEmperor")
   

ctx.ServiceManager
#-------------------------------------

I get an error "__main__.IllegalArgumentException", on the
xMasterPropSet.setPropertyValue("Name", "UserEmperor") line.
If I run dir(xMasterPropSet), I get all the expected method and
properties, so it just look like the setPropertyValue is either not the
implementation I expected, or that the argument types are wrong?

Can anyone give me a hint on how to do that?

Thanks a lot!.
-Etienne



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: PyUNO : Inserting a User field

Fernand Vanrie
Etienne
in basic its done like:
Dim Doc As Object
Dim DateTimeField As Object
Dim Cursor As Object
Doc = StarDesktop.CurrentComponent
Cursor = Doc.Text.createTextCursor()
DateTimeField =
Doc.createInstance("com.sun.star.text.T"com.sun.star.text.TextField.User")")

Doc.Text.insertTextContent(Cursor, DateTimeField, False)

hope it helps
fernand

> Dear list,
>
> First I hope I am in the right place... Please forgive me and direct
> me to the appropriate mailing-list/forum/whatever if I'm off-topic...
>
> I'm trying to develop an application that would interact with
> OpenOffice via PyUNO, and I am not able to create a User field. I
> started from the Java example here:
> http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Fields,
> and tried to convert it in Python...
>
> Dim Doc As Object
> Dim DateTimeField As Object
> Dim Cursor As Object
> Doc = StarDesktop.CurrentComponent
> Cursor = Doc.Text.createTextCursor()
> DateTimeField =
> Doc.createInstance("com.sun.star.text.T"com.sun.star.text.TextField.User")")
>
> Doc.Text.insertTextContent(Cursor, DateTimeField, False)
> #-------------------------------------
> import uno
>
> localContext = uno.getComponentContext()
> resolver =
> localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
> localContext )
> ctx = resolver.resolve(
> "uno:pipe,name=OOo_pipe;urp;StarOffice.ComponentContext" )
> smgr = ctx.ServiceManager
>
> desktop = smgr.createInstanceWithContext(
> "com.sun.star.frame.Desktop",ctx)
> model   = desktop.getCurrentComponent()
>
> if model.getImplementationName()=='SwXTextDocument':
>    xUserField = model.createInstance("com.sun.star.text.TextField.User")
>    xMasterPropSet =
> model.createInstance("com.sun.star.text.FieldMaster.User")
>    xMasterPropSet.setPropertyValue("Name", "UserEmperor")
>  
> ctx.ServiceManager
> #-------------------------------------
>
> I get an error "__main__.IllegalArgumentException", on the
> xMasterPropSet.setPropertyValue("Name", "UserEmperor") line.
> If I run dir(xMasterPropSet), I get all the expected method and
> properties, so it just look like the setPropertyValue is either not
> the implementation I expected, or that the argument types are wrong?
>
> Can anyone give me a hint on how to do that?
>
> Thanks a lot!.
> -Etienne
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: PyUNO : Inserting a User field

et.gaudrain
Thank you very much Fernand.
Can you please show how you assign a Name and Content to the field in Basic?
The creation of the field seems okay, but the glitch appears when I would to put something in it.

Thanks again,
-Etienne


----- Mail Original -----
De: "Fernand Vanrie" <[hidden email]>
À: [hidden email]
Envoyé: Jeudi 22 Octobre 2009 19h01:11 GMT +00:00 GMT - Grande-Bretagne, Irlande, Portugal
Objet: Re: [udk-dev] PyUNO : Inserting a User field

Etienne
in basic its done like:
Dim Doc As Object
Dim DateTimeField As Object
Dim Cursor As Object
Doc = StarDesktop.CurrentComponent
Cursor = Doc.Text.createTextCursor()
DateTimeField =
Doc.createInstance("com.sun.star.text.T"com.sun.star.text.TextField.User")")

Doc.Text.insertTextContent(Cursor, DateTimeField, False)

hope it helps
fernand

> Dear list,
>
> First I hope I am in the right place... Please forgive me and direct
> me to the appropriate mailing-list/forum/whatever if I'm off-topic...
>
> I'm trying to develop an application that would interact with
> OpenOffice via PyUNO, and I am not able to create a User field. I
> started from the Java example here:
> http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Fields,
> and tried to convert it in Python...
>
> Dim Doc As Object
> Dim DateTimeField As Object
> Dim Cursor As Object
> Doc = StarDesktop.CurrentComponent
> Cursor = Doc.Text.createTextCursor()
> DateTimeField =
> Doc.createInstance("com.sun.star.text.T"com.sun.star.text.TextField.User")")
>
> Doc.Text.insertTextContent(Cursor, DateTimeField, False)
> #-------------------------------------
> import uno
>
> localContext = uno.getComponentContext()
> resolver =
> localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
> localContext )
> ctx = resolver.resolve(
> "uno:pipe,name=OOo_pipe;urp;StarOffice.ComponentContext" )
> smgr = ctx.ServiceManager
>
> desktop = smgr.createInstanceWithContext(
> "com.sun.star.frame.Desktop",ctx)
> model   = desktop.getCurrentComponent()
>
> if model.getImplementationName()=='SwXTextDocument':
>    xUserField = model.createInstance("com.sun.star.text.TextField.User")
>    xMasterPropSet =
> model.createInstance("com.sun.star.text.FieldMaster.User")
>    xMasterPropSet.setPropertyValue("Name", "UserEmperor")
>  
> ctx.ServiceManager
> #-------------------------------------
>
> I get an error "__main__.IllegalArgumentException", on the
> xMasterPropSet.setPropertyValue("Name", "UserEmperor") line.
> If I run dir(xMasterPropSet), I get all the expected method and
> properties, so it just look like the setPropertyValue is either not
> the implementation I expected, or that the argument types are wrong?
>
> Can anyone give me a hint on how to do that?
>
> Thanks a lot!.
> -Etienne
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: PyUNO : Inserting a User field

et.gaudrain
In reply to this post by et.gaudrain
After a few more attempts, its seems that:

xMasterPropSet.setPropertyValue("Content", "Toto")

works perfectly fine.
So this is the use of "Name" as a property that is not allowed in
setPropertyValue.

Any hint?

-Etienne




Etienne Gaudrain wrote:

> Dear list,
>
> First I hope I am in the right place... Please forgive me and direct
> me to the appropriate mailing-list/forum/whatever if I'm off-topic...
>
> I'm trying to develop an application that would interact with
> OpenOffice via PyUNO, and I am not able to create a User field. I
> started from the Java example here:
> http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Fields,
> and tried to convert it in Python...
>
> Here is my code:
>
> #-------------------------------------
> import uno
>
> localContext = uno.getComponentContext()
> resolver =
> localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
> localContext )
> ctx = resolver.resolve(
> "uno:pipe,name=OOo_pipe;urp;StarOffice.ComponentContext" )
> smgr = ctx.ServiceManager
>
> desktop = smgr.createInstanceWithContext(
> "com.sun.star.frame.Desktop",ctx)
> model   = desktop.getCurrentComponent()
>
> if model.getImplementationName()=='SwXTextDocument':
>    xUserField = model.createInstance("com.sun.star.text.TextField.User")
>    xMasterPropSet =
> model.createInstance("com.sun.star.text.FieldMaster.User")
>    xMasterPropSet.setPropertyValue("Name", "UserEmperor")
>  
> ctx.ServiceManager
> #-------------------------------------
>
> I get an error "__main__.IllegalArgumentException", on the
> xMasterPropSet.setPropertyValue("Name", "UserEmperor") line.
> If I run dir(xMasterPropSet), I get all the expected method and
> properties, so it just look like the setPropertyValue is either not
> the implementation I expected, or that the argument types are wrong?
>
> Can anyone give me a hint on how to do that?
>
> Thanks a lot!.
> -Etienne
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Etienne Gaudrain
Centre for the Neural Basis of Hearing
Department of Physiology, Development and Neuroscience
University of Cambridge
Downing Street
Cambridge CB2 3EG
UK
Phone: +44 1223 765 359 office
Fax: +44 1223 333 840 department
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: PyUNO : Inserting a User field

stephan.bergmann
On 10/23/09 11:47, Etienne Gaudrain wrote:
> After a few more attempts, its seems that:
>
> xMasterPropSet.setPropertyValue("Content", "Toto")
>
> works perfectly fine.
> So this is the use of "Name" as a property that is not allowed in
> setPropertyValue.

Each UNO object implementing XPropertySet has its own rules what kinds
of property names (and corresponding values) it supports.  It might well
be that a com.sun.star.text.FieldMaster.User does not support a "Name"
property, but I do not know
(offapi/com/sun/star/text/fieldmaster/User.idl says that it inherits
from offapi/com/sun/star/text/TextFieldMaster.idl, which only has an
optional Name property, so that might give a clue).
[hidden email] would be the right mailing list for such questions.

-Stephan

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: PyUNO : Inserting a User field

et.gaudrain
In reply to this post by et.gaudrain
I found this in the API doc of TextFieldMaster:

Name: determins the name of the field master. The name is void as long
as the instance is not member of the document structure. When the value
is being set the instance is inserted into the document and the name
cannot be changed afterwards. That does not apply to the Database text
field master.

So do you think the object is inserted into the document when I
instantiate it (with an empty Name)? How can I instantiate without
inserting in the document?

Thanks,
-Etienne


Etienne Gaudrain wrote:

> After a few more attempts, its seems that:
>
> xMasterPropSet.setPropertyValue("Content", "Toto")
>
> works perfectly fine.
> So this is the use of "Name" as a property that is not allowed in
> setPropertyValue.
>
> Any hint?
>
> -Etienne
>
>
>
>
> Etienne Gaudrain wrote:
>> Dear list,
>>
>> First I hope I am in the right place... Please forgive me and direct
>> me to the appropriate mailing-list/forum/whatever if I'm off-topic...
>>
>> I'm trying to develop an application that would interact with
>> OpenOffice via PyUNO, and I am not able to create a User field. I
>> started from the Java example here:
>> http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Fields,
>> and tried to convert it in Python...
>>
>> Here is my code:
>>
>> #-------------------------------------
>> import uno
>>
>> localContext = uno.getComponentContext()
>> resolver =
>> localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
>> localContext )
>> ctx = resolver.resolve(
>> "uno:pipe,name=OOo_pipe;urp;StarOffice.ComponentContext" )
>> smgr = ctx.ServiceManager
>>
>> desktop = smgr.createInstanceWithContext(
>> "com.sun.star.frame.Desktop",ctx)
>> model   = desktop.getCurrentComponent()
>>
>> if model.getImplementationName()=='SwXTextDocument':
>>    xUserField = model.createInstance("com.sun.star.text.TextField.User")
>>    xMasterPropSet =
>> model.createInstance("com.sun.star.text.FieldMaster.User")
>>    xMasterPropSet.setPropertyValue("Name", "UserEmperor")
>>  
>> ctx.ServiceManager
>> #-------------------------------------
>>
>> I get an error "__main__.IllegalArgumentException", on the
>> xMasterPropSet.setPropertyValue("Name", "UserEmperor") line.
>> If I run dir(xMasterPropSet), I get all the expected method and
>> properties, so it just look like the setPropertyValue is either not
>> the implementation I expected, or that the argument types are wrong?
>>
>> Can anyone give me a hint on how to do that?
>>
>> Thanks a lot!.
>> -Etienne
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>
>


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Etienne Gaudrain
Centre for the Neural Basis of Hearing
Department of Physiology, Development and Neuroscience
University of Cambridge
Downing Street
Cambridge CB2 3EG
UK
Phone: +44 1223 765 359 office
Fax: +44 1223 333 840 department
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: PyUNO : Inserting a User field

Fernand Vanrie
In reply to this post by et.gaudrain
Etienne,
 
I used in the past  this code: (  founded on the OO-forum with thanks to
JonnV:-)
hope it helps
Fernand

  sub InsertUserField
   oDoc = ThisComponent
   
   oField = oDoc.createInstance("com.sun.star.text.FieldMaster.User")
   oField.Name  = "New field13" 'need to be Unique
   oField.Value = "New Field Text" 'your text

   objField = oDoc.createInstance("com.sun.star.text.TextField.User")
   objField.attachTextFieldMaster(oField)

   oVC = oDoc.CurrentController.getViewCursor
   oDoc.Text.insertTextContent(oVC,objField,false)
end sub

 

> Thank you very much Fernand.
> Can you please show how you assign a Name and Content to the field in Basic?
> The creation of the field seems okay, but the glitch appears when I would to put something in it.
>
> Thanks again,
> -Etienne
>
>
> ----- Mail Original -----
> De: "Fernand Vanrie" <[hidden email]>
> À: [hidden email]
> Envoyé: Jeudi 22 Octobre 2009 19h01:11 GMT +00:00 GMT - Grande-Bretagne, Irlande, Portugal
> Objet: Re: [udk-dev] PyUNO : Inserting a User field
>
> Etienne
> in basic its done like:
> Dim Doc As Object
> Dim DateTimeField As Object
> Dim Cursor As Object
> Doc = StarDesktop.CurrentComponent
> Cursor = Doc.Text.createTextCursor()
> DateTimeField =
> Doc.createInstance("com.sun.star.text.T"com.sun.star.text.TextField.User")")
>
> Doc.Text.insertTextContent(Cursor, DateTimeField, False)
>
> hope it helps
> fernand
>  
>> Dear list,
>>
>> First I hope I am in the right place... Please forgive me and direct
>> me to the appropriate mailing-list/forum/whatever if I'm off-topic...
>>
>> I'm trying to develop an application that would interact with
>> OpenOffice via PyUNO, and I am not able to create a User field. I
>> started from the Java example here:
>> http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Fields,
>> and tried to convert it in Python...
>>
>> Dim Doc As Object
>> Dim DateTimeField As Object
>> Dim Cursor As Object
>> Doc = StarDesktop.CurrentComponent
>> Cursor = Doc.Text.createTextCursor()
>> DateTimeField =
>> Doc.createInstance("com.sun.star.text.T"com.sun.star.text.TextField.User")")
>>
>> Doc.Text.insertTextContent(Cursor, DateTimeField, False)
>> #-------------------------------------
>> import uno
>>
>> localContext = uno.getComponentContext()
>> resolver =
>> localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
>> localContext )
>> ctx = resolver.resolve(
>> "uno:pipe,name=OOo_pipe;urp;StarOffice.ComponentContext" )
>> smgr = ctx.ServiceManager
>>
>> desktop = smgr.createInstanceWithContext(
>> "com.sun.star.frame.Desktop",ctx)
>> model   = desktop.getCurrentComponent()
>>
>> if model.getImplementationName()=='SwXTextDocument':
>>    xUserField = model.createInstance("com.sun.star.text.TextField.User")
>>    xMasterPropSet =
>> model.createInstance("com.sun.star.text.FieldMaster.User")
>>    xMasterPropSet.setPropertyValue("Name", "UserEmperor")
>>  
>> ctx.ServiceManager
>> #-------------------------------------
>>
>> I get an error "__main__.IllegalArgumentException", on the
>> xMasterPropSet.setPropertyValue("Name", "UserEmperor") line.
>> If I run dir(xMasterPropSet), I get all the expected method and
>> properties, so it just look like the setPropertyValue is either not
>> the implementation I expected, or that the argument types are wrong?
>>
>> Can anyone give me a hint on how to do that?
>>
>> Thanks a lot!.
>> -Etienne
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>    
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: PyUNO : Inserting a User field

et.gaudrain
Okay, got it!

The problem I had was actually that the name I used was not unique, I
mean not unique anymore after many trials in the same document.

Thanks a lot to all of you!
-Etienne


Fernand Vanrie wrote:

> Etienne,
>
> I used in the past  this code: (  founded on the OO-forum with thanks
> to JonnV:-)
> hope it helps
> Fernand
>
>  sub InsertUserField
>   oDoc = ThisComponent
>     oField = oDoc.createInstance("com.sun.star.text.FieldMaster.User")
>   oField.Name  = "New field13" 'need to be Unique
>   oField.Value = "New Field Text" 'your text
>
>   objField = oDoc.createInstance("com.sun.star.text.TextField.User")
>   objField.attachTextFieldMaster(oField)
>
>   oVC = oDoc.CurrentController.getViewCursor
>   oDoc.Text.insertTextContent(oVC,objField,false)
> end sub
>
>
>
>> Thank you very much Fernand.
>> Can you please show how you assign a Name and Content to the field in
>> Basic?
>> The creation of the field seems okay, but the glitch appears when I
>> would to put something in it.
>>
>> Thanks again,
>> -Etienne
>>
>>
>> ----- Mail Original -----
>> De: "Fernand Vanrie" <[hidden email]>
>> À: [hidden email]
>> Envoyé: Jeudi 22 Octobre 2009 19h01:11 GMT +00:00 GMT -
>> Grande-Bretagne, Irlande, Portugal
>> Objet: Re: [udk-dev] PyUNO : Inserting a User field
>>
>> Etienne
>> in basic its done like:
>> Dim Doc As Object
>> Dim DateTimeField As Object
>> Dim Cursor As Object
>> Doc = StarDesktop.CurrentComponent
>> Cursor = Doc.Text.createTextCursor()
>> DateTimeField =
>> Doc.createInstance("com.sun.star.text.T"com.sun.star.text.TextField.User")")
>>
>> Doc.Text.insertTextContent(Cursor, DateTimeField, False)
>>
>> hope it helps
>> fernand
>>  
>>> Dear list,
>>>
>>> First I hope I am in the right place... Please forgive me and direct
>>> me to the appropriate mailing-list/forum/whatever if I'm off-topic...
>>>
>>> I'm trying to develop an application that would interact with
>>> OpenOffice via PyUNO, and I am not able to create a User field. I
>>> started from the Java example here:
>>> http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Fields,
>>> and tried to convert it in Python...
>>>
>>> Dim Doc As Object
>>> Dim DateTimeField As Object
>>> Dim Cursor As Object
>>> Doc = StarDesktop.CurrentComponent
>>> Cursor = Doc.Text.createTextCursor()
>>> DateTimeField =
>>> Doc.createInstance("com.sun.star.text.T"com.sun.star.text.TextField.User")")
>>>
>>> Doc.Text.insertTextContent(Cursor, DateTimeField, False)
>>> #-------------------------------------
>>> import uno
>>>
>>> localContext = uno.getComponentContext()
>>> resolver =
>>> localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
>>> localContext )
>>> ctx = resolver.resolve(
>>> "uno:pipe,name=OOo_pipe;urp;StarOffice.ComponentContext" )
>>> smgr = ctx.ServiceManager
>>>
>>> desktop = smgr.createInstanceWithContext(
>>> "com.sun.star.frame.Desktop",ctx)
>>> model   = desktop.getCurrentComponent()
>>>
>>> if model.getImplementationName()=='SwXTextDocument':
>>>    xUserField =
>>> model.createInstance("com.sun.star.text.TextField.User")
>>>    xMasterPropSet =
>>> model.createInstance("com.sun.star.text.FieldMaster.User")
>>>    xMasterPropSet.setPropertyValue("Name", "UserEmperor")
>>>  
>>> ctx.ServiceManager
>>> #-------------------------------------
>>>
>>> I get an error "__main__.IllegalArgumentException", on the
>>> xMasterPropSet.setPropertyValue("Name", "UserEmperor") line.
>>> If I run dir(xMasterPropSet), I get all the expected method and
>>> properties, so it just look like the setPropertyValue is either not
>>> the implementation I expected, or that the argument types are wrong?
>>>
>>> Can anyone give me a hint on how to do that?
>>>
>>> Thanks a lot!.
>>> -Etienne
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>    
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>  
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Etienne Gaudrain
Centre for the Neural Basis of Hearing
Department of Physiology, Development and Neuroscience
University of Cambridge
Downing Street
Cambridge CB2 3EG
UK
Phone: +44 1223 765 359 office
Fax: +44 1223 333 840 department
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: PyUNO : Inserting a User field

et.gaudrain
Here is the full working code:

#------------------------------------------------------
import uno

localContext = uno.getComponentContext()
resolver =
localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
localContext )
ctx = resolver.resolve(
"uno:pipe,name=OOo_pipe;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager

desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
model   = desktop.getCurrentComponent()

def createCustomField(name, content):
   
    masterNames = model.TextFieldMasters.ElementNames
    masterUserNames = list()
    for m in masterNames:
        if m[0:35] == 'com.sun.star.text.fieldmaster.User.':
            masterUserNames.append(m)
   
    tfmsU = [model.TextFieldMasters.getByName(x) for x in masterUserNames]
    masterUserNames = [x[35:] for x in masterUserNames]
   
    print masterUserNames
   
    if name in masterUserNames:
        print '%s already exists...' % name
        return None
   
    xMaster = model.createInstance("com.sun.star.text.fieldmaster.User")
    xMaster.Name = name
    xMaster.Content = content
   
    xUserField = model.createInstance("com.sun.star.text.textfield.User")
    xUserField.attachTextFieldMaster(xMaster)
   
    return xUserField

def updateField(name, content):
    tfm =
model.TextFieldMasters.getByName('com.sun.star.text.fieldmaster.User.%s'
% name)
    tfm.setPropertyValue("Content", content)
    for f in tfm.DependentTextFields:
        f.update()

if model.getImplementationName()=='SwXTextDocument':

    f = createCustomField("MyField1", "This is MyField")
   
    if f is not None:
        model.Text.insertTextContent(model.Text.getEnd(), f, False)
       
    updateField("MyField1", "Modified")



ctx.ServiceManager
#------------------------------------------------------


Etienne Gaudrain wrote:

> Okay, got it!
>
> The problem I had was actually that the name I used was not unique, I
> mean not unique anymore after many trials in the same document.
>
> Thanks a lot to all of you!
> -Etienne
>
>
> Fernand Vanrie wrote:
>> Etienne,
>>
>> I used in the past  this code: (  founded on the OO-forum with thanks
>> to JonnV:-)
>> hope it helps
>> Fernand
>>
>>  sub InsertUserField
>>   oDoc = ThisComponent
>>     oField = oDoc.createInstance("com.sun.star.text.FieldMaster.User")
>>   oField.Name  = "New field13" 'need to be Unique
>>   oField.Value = "New Field Text" 'your text
>>
>>   objField = oDoc.createInstance("com.sun.star.text.TextField.User")
>>   objField.attachTextFieldMaster(oField)
>>
>>   oVC = oDoc.CurrentController.getViewCursor
>>   oDoc.Text.insertTextContent(oVC,objField,false)
>> end sub
>>
>>
>>
>>> Thank you very much Fernand.
>>> Can you please show how you assign a Name and Content to the field
>>> in Basic?
>>> The creation of the field seems okay, but the glitch appears when I
>>> would to put something in it.
>>>
>>> Thanks again,
>>> -Etienne
>>>
>>>
>>> ----- Mail Original -----
>>> De: "Fernand Vanrie" <[hidden email]>
>>> À: [hidden email]
>>> Envoyé: Jeudi 22 Octobre 2009 19h01:11 GMT +00:00 GMT -
>>> Grande-Bretagne, Irlande, Portugal
>>> Objet: Re: [udk-dev] PyUNO : Inserting a User field
>>>
>>> Etienne
>>> in basic its done like:
>>> Dim Doc As Object
>>> Dim DateTimeField As Object
>>> Dim Cursor As Object
>>> Doc = StarDesktop.CurrentComponent
>>> Cursor = Doc.Text.createTextCursor()
>>> DateTimeField =
>>> Doc.createInstance("com.sun.star.text.T"com.sun.star.text.TextField.User")")
>>>
>>> Doc.Text.insertTextContent(Cursor, DateTimeField, False)
>>>
>>> hope it helps
>>> fernand
>>>  
>>>> Dear list,
>>>>
>>>> First I hope I am in the right place... Please forgive me and
>>>> direct me to the appropriate mailing-list/forum/whatever if I'm
>>>> off-topic...
>>>>
>>>> I'm trying to develop an application that would interact with
>>>> OpenOffice via PyUNO, and I am not able to create a User field. I
>>>> started from the Java example here:
>>>> http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Fields,
>>>> and tried to convert it in Python...
>>>>
>>>> Dim Doc As Object
>>>> Dim DateTimeField As Object
>>>> Dim Cursor As Object
>>>> Doc = StarDesktop.CurrentComponent
>>>> Cursor = Doc.Text.createTextCursor()
>>>> DateTimeField =
>>>> Doc.createInstance("com.sun.star.text.T"com.sun.star.text.TextField.User")")
>>>>
>>>> Doc.Text.insertTextContent(Cursor, DateTimeField, False)
>>>> #-------------------------------------
>>>> import uno
>>>>
>>>> localContext = uno.getComponentContext()
>>>> resolver =
>>>> localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
>>>> localContext )
>>>> ctx = resolver.resolve(
>>>> "uno:pipe,name=OOo_pipe;urp;StarOffice.ComponentContext" )
>>>> smgr = ctx.ServiceManager
>>>>
>>>> desktop = smgr.createInstanceWithContext(
>>>> "com.sun.star.frame.Desktop",ctx)
>>>> model   = desktop.getCurrentComponent()
>>>>
>>>> if model.getImplementationName()=='SwXTextDocument':
>>>>    xUserField =
>>>> model.createInstance("com.sun.star.text.TextField.User")
>>>>    xMasterPropSet =
>>>> model.createInstance("com.sun.star.text.FieldMaster.User")
>>>>    xMasterPropSet.setPropertyValue("Name", "UserEmperor")
>>>>  
>>>> ctx.ServiceManager
>>>> #-------------------------------------
>>>>
>>>> I get an error "__main__.IllegalArgumentException", on the
>>>> xMasterPropSet.setPropertyValue("Name", "UserEmperor") line.
>>>> If I run dir(xMasterPropSet), I get all the expected method and
>>>> properties, so it just look like the setPropertyValue is either not
>>>> the implementation I expected, or that the argument types are wrong?
>>>>
>>>> Can anyone give me a hint on how to do that?
>>>>
>>>> Thanks a lot!.
>>>> -Etienne
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [hidden email]
>>>> For additional commands, e-mail: [hidden email]
>>>>    
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>  
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>
>


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Etienne Gaudrain
Centre for the Neural Basis of Hearing
Department of Physiology, Development and Neuroscience
University of Cambridge
Downing Street
Cambridge CB2 3EG
UK
Phone: +44 1223 765 359 office
Fax: +44 1223 333 840 department
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]