Ask Your Question
0

How to access document variables from basic macro? [closed]

asked 2015-12-08 12:08:06 +0200

Jackal gravatar image

I find no single way in any documentation to archieve this:

A text document in Writer contains a contract. It also contains a basic macro with dialog where this contract can be configured. It asks for names, values, dates, and provides list boxes with predefined options to choose from. After hitting OK, the macro will a) put the names into predefined placeholders (my way was to add a field that displays a variable) b) switch on and off several hidden paragraphs (here, they also react on well-defined variable conditions).

So, in short, I want to access that "document variables" from my macro. Googeling anything that should make sense led to massive amounts of useless information, also because there is no real way to distinguish variables that are defined by Insert -> Fields -> More Fields -> Variables -> Set Variable and classic variables like "Public name as String".

I tried to record a macro where I manually edit a set-variable-field and hoped it would show me how to access it and put in a new value, but that resulting macro didn't reflect this step.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2016-03-12 19:56:36.893766

1 Answer

Sort by » oldest newest most voted
0

answered 2015-12-10 14:45:36 +0200

pierre-yves samyn gravatar image

Hi

The UpdateVar.odt attached includes:

  • Variable MyVarBool (Boolean format)
  • Variable MyVarText (Text format)
  • A hidden conditional section (Hide with condition: MyVarBool)

The program below:

  • Checks for the variable, if exists...
  • Puts the current time in the variable MyVarText
  • Switches the MyVarBool variable true / false

The section is displayed or not based on the contents of the variable MyVarBool (see FormatSection)

Sub UpdateVariable
dim oDoc as object, oVar as object

oDoc =ThisComponent

if oDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.MyVarText") then
    oVar = oDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.MyVarText")
    oVar.DependentTextFields(0).content = time
else
    msgbox "MyVarText not found"
end if

if oDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.MyVarBool") then
    oVar = oDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.MyVarBool")  
    with oVar.DependentTextFields(0)
        if .Value = 0 then 
            .content = "TRUE"
        else
            .content = "FALSE"
        end if  
    end with    
else
    msgbox "MyVarBool not found"
end if

oDoc.textFields.refresh

end sub

Regards

edit flag offensive delete link more

Comments

I thank you very much for your explaination and put it to the test if it's a viable solution. Sadly, it's so clumpsy solving my problem on this way, that I refused using LO but tried HTML5 and PhantomJS, where I have full DOM / Document access. That kind of features seem to be missing here, maybe for the sake to be as much like M$ as possible. Thanks a lot, though!

Jackal gravatar imageJackal ( 2015-12-18 19:00:55 +0200 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-08 12:02:02 +0200

Seen: 878 times

Last updated: Dec 10 '15