Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9137277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:05:01+00:00 2026-06-17T09:05:01+00:00

I’m trying to define variables in an openoffice document, and I must be doing

  • 0

I’m trying to define “variables” in an openoffice document, and I must be doing something wrong because when I try to display the value of the variable using a field, I only get an empty string.

Here’s the code I’m using (using the Python UNO bridge). The interesting bit is the second function.

import time
import subprocess
import logging
import os
import sys
import uno
from com.sun.star.text.SetVariableType import STRING

def get_uno_model(): # helper function to connect to OOo. Only interesting 
                     # if you want to reproduce the issue locally, 
                     # don't spend time on this one
    try:
        model = XSCRIPTCONTEXT.getDocument()
    except NameError:
        pass # we are not running in a macro
    # get the uno component context from the PyUNO runtime
    localContext = uno.getComponentContext()

    # create the UnoUrlResolver
    resolver = localContext.ServiceManager.createInstanceWithContext(
                                    "com.sun.star.bridge.UnoUrlResolver", localContext )

    # connect to the running office
    try:
        ctx = resolver.resolve("uno:socket,host=localhost,port=2002;"
                               "urp;StarOffice.ComponentContext")
    except:
        cmd = ['soffice', '--writer', '-accept=socket,host=localhost,port=2002;urp;']
        popen = subprocess.Popen(cmd)
        time.sleep(1)
        ctx = resolver.resolve("uno:socket,host=localhost,port=2002;"
                               "urp;StarOffice.ComponentContext")
    smgr = ctx.ServiceManager

    # get the central desktop object
    desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

    # access the current writer document
    model = desktop.getCurrentComponent()
    return model

def build_variable(model, name, value):
    # find or create a TextFieldMaster with the correct name
    master_prefix = u"com.sun.star.text.fieldmaster.SetExpression"
    variable_names = set([_name.split('.')[-1] 
                          for _name in model.TextFieldMasters.ElementNames
                          if _name.startswith(master_prefix)])
    master_name = u'%s.%s' % (master_prefix, name)
    if name not in variable_names:
        master = model.createInstance(master_prefix)
        master.Name = name
    else:
        master = model.TextFieldMasters.getByName(master_name)

    # create the SetExpression field
    field = model.createInstance(u'com.sun.star.text.textfield.SetExpression')
    field.attachTextFieldMaster(master)
    field.IsVisible = True
    field.IsInput = False
    field.SubType = STRING
    field.Content = value
    return field

model = get_uno_model() # local function to connect to OpenOffice
text = model.Text
field = build_variable(model, u'Toto', 'nice variable')
text.insertTextContent(text.getEnd(), field, False)

This code works somehow (unless I removed too much), but if I manually insert a field to display the value of Toto I don’t get the ‘nice variable’ string that I expect, and the field which is inserted has no value

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:05:02+00:00Added an answer on June 17, 2026 at 9:05 am

    The code is missing setting the SubType property of the master field to STRING after the creation of the master field.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.