OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Change user-defined variable in writer
Goto page 1, 2  Next
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Belfurious
Guest





PostPosted: Tue Dec 16, 2003 9:41 am    Post subject: Change user-defined variable in writer Reply with quote

I want to create a macro that will show hidden text when I click a button on a form.

I have figured out how to manually show/hide hidden text with a user-defined variable. I also know how to create a button and link it to a macro.

My question is this: how do I program a MACRO to modify the user-defined variable in the document? In other words, how do I reference the variable in the macro, and then modify its value?

Also, if there's a better way to solve this problem than with a user-defined variable, I'm open to that too.

Thanks!
Back to top
dfrench
Moderator
Moderator


Joined: 03 Mar 2003
Posts: 1605
Location: Wellington, New Zealand

PostPosted: Tue Dec 16, 2003 11:00 am    Post subject: Reply with quote

Have a look at Andrew Pitonyak's macros (http://www.ooomacros.org/files.php?type=doc). This snippet sets the first userfield (Info1) ....
Code:
vdoc =thiscomponent
vInfo = vDoc.getDocumentInfo()
vInfo.setUserFieldValue(0,"MYFIELDVALUE")

You will need to iterate through the fields to find the one that you wish to set
Quote:
if there's a better way to solve this problem than with a user-defined variable
Seems a good and intuitive approach for documents that you have control of and are not changing (in the hidden text) much. If you needed to avoid distributing the hidden text then you could use bookmarks at the places where text is to be optionally exposed and simply insert text at that point through the macro code.
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Tue Dec 16, 2003 4:01 pm    Post subject: Reply with quote

The Q & A present an interesting problem. I'm going to assume the OP was using either a Hidden Paragraph or Conditional Text based on the value of a User Variable defined at Ctrl+F2 > Variable Tab which works just fine.

However, if I set Info 1 at File > Properties > User Defined tab then it can be inserted into the doc with Ctrl+F2 > Doc Info tab > Info and the View > Fields shows it as "DocInformation:Info 1" but I can't get Conditional Text to recognize this variable with the Condition set to: DocInformation:Info 1 EQ "a". Of course, Basic doesn't normally allow variable names with spaces.

I tried a bunch of different stuff but was unsuccessfull. Depending on what I did I got both true and false responses but never one that would change its value correctly.
Back to top
View user's profile Send private message
dfrench
Moderator
Moderator


Joined: 03 Mar 2003
Posts: 1605
Location: Wellington, New Zealand

PostPosted: Tue Dec 16, 2003 8:31 pm    Post subject: Reply with quote

You can change the name "Info 1" to anything else
Code:
vdoc =thiscomponent
vInfo = vDoc.getDocumentInfo()
vInfo.setUserFieldValue(0,"MYFIELDVALUE")
vInfo.setUserFieldName(0,"MYVARIABLENAME")

or, if you are using user defined fields , then this code can set those fields
Code:
sub change
   vDoc = ThisComponent
   Dim vTextFieldMaster
   sUserType = "com.sun.star.text.FieldMaster.User"
   vVal = vDoc.getTextFieldMasters()
skey = "com.sun.star.text.FieldMaster.User." & "MYVARIABLENAME"
vTextFieldMaster = vVal.getByName(sKey)
vTextFieldMaster.Content = "MYVALUE"
end sub

I hope one of these has the desired effect on HIDDEN TEXT or HIDDEN PARAGRAPH Smile
Back to top
View user's profile Send private message
Belfurious
Guest





PostPosted: Tue Dec 16, 2003 10:20 pm    Post subject: Reply with quote

Thanks for the replies! dfrench, the macros you present work beautifully.

Unfortunately, as JohnV aludes to, they don't affect the hidden text.

The hidden text IS affected by the variable I define with Insert-->Fields-->Other (or Ctrl+F2), then selecting "Set variable", and entering a name and value.

The user defined fields in the Properties dialogue (Info 1, Info 2, etc.), have no effect on the hiddent text.

So can you tell me how to change the value of the "Set variable" with a macro?

Thanks again!
Back to top
dfrench
Moderator
Moderator


Joined: 03 Mar 2003
Posts: 1605
Location: Wellington, New Zealand

PostPosted: Tue Dec 16, 2003 10:41 pm    Post subject: Reply with quote

Change "com.sun.star.text.FieldMaster.User" to
"com.sun.star.text.FieldMaster.SetExpression"

best regards
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Wed Dec 17, 2003 6:29 am    Post subject: Reply with quote

Hi David,

One of the many things I tried with the field "Info 1" was to change its name. Didn't seem to work. I also tried surrounding "DocInformation:Info 1" with quotes, single quotes, and < >, again without apparent success. It seems there should be a way to reference this field with Conditional Text, etc.

Thanks for the code for User & Set Variable fields. I'm sure I will use that in the future. I've actually had use for it in the past but worked around it in other ways.

As chance would have it, I needed to use "Info 1" in code the other day and managed, with the help of Xray, to figure out how to "set" & "get" this one as well as "Keywords". Very useful in the right context because it doesn't require that the variable be inserted in the doc.

Belfurious,

FWIW, I've spent a lot of time with Set Variable vs User and find that I like the overall way User works much better. (But then I've never designed a template where I wanted the ability to change the definition of a variable from point X onward instead of globally. )
Back to top
View user's profile Send private message
Belfurious
Guest





PostPosted: Wed Dec 17, 2003 6:46 am    Post subject: Reply with quote

Okay, I'm getting closer!

When I run the macro:

Code:

sub change
   vDoc = ThisComponent
   Dim vTextFieldMaster
   sUserType = "com.sun.star.text.FieldMaster.SetExpression"
   vVal = vDoc.getTextFieldMasters()
skey = "com.sun.star.text.FieldMaster.SetExpression." & "MYVARIABLE"
vTextFieldMaster = vVal.getByName(sKey)
vTextFieldMaster.Content = "MYVALUE"
end sub


I get a runtime error message on the last line. Can you tell me what I'm doing wrong?

Just to clarify, here's what my document looks like:

*Beginning of document*

<field code>
Set variable MYVARIABLE = 0
</field code>

The above variable toggles the hidden text below to show or hide.

I want a macro linked to the button below to change the variable above to 1.

*This is a Button*

<field code>
Hidden text MYVARIABLE EQ 0 This is hidden text.
</field /code>

*End of Document*

If I manually set MYVARIABLE to 1, the hidden text shows. That's what I want the macro to do. Thanks!
Back to top
dfrench
Moderator
Moderator


Joined: 03 Mar 2003
Posts: 1605
Location: Wellington, New Zealand

PostPosted: Wed Dec 17, 2003 2:50 pm    Post subject: Reply with quote

How about registering and sending me the document so we are both on the same page?
Back to top
View user's profile Send private message
dfrench
Moderator
Moderator


Joined: 03 Mar 2003
Posts: 1605
Location: Wellington, New Zealand

PostPosted: Wed Dec 17, 2003 4:08 pm    Post subject: Reply with quote

ok ... bit of a hack here. Happy to see a better piece of code Smile
Assuming a hidden text condition of 'show ==0'
This code locates the set variable field by name 'show' ; and toggles value between 0 and 1
Code:
sub enumfields
' updates the field value
Dim Doc As Object
Dim TextFieldEnum As Object
Dim TextField As Object
Dim I As Integer
Doc = Thiscomponent
TextFieldEnum = Doc.getTextFields.createEnumeration  ' all fields in document

While TextFieldEnum.hasMoreElements()
   TextField = TextFieldEnum.nextElement()  ' find the fields
   Master = Textfield.gettextfieldmaster()  ' get the master to get the field name
   If master.name="show" then               ' chosen field name
      if textfield.content = 1 then
         textfield.content = 0
      else
         textfield.content = 1
                           ' set value
      end if
   end if
Wend
end sub

To see the effect on screen you have to update fields. The recorded code below will do that.
Code:
sub udfield
rem --recorded macro for updating fields
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")

rem ----------------------------------------------------------------------
 dispatcher.executeDispatch(document, ".uno:UpdateFields", "", 0, Array())


end sub

Apologies for the confusion ... user variables; user fields etc these are something different again
<<<UPDATE>>>>
in case anyone uses this as an example, the code to 'update fields' can be replaced entirely by
Code:
thiscomponent.getTextFields.refresh()


Last edited by dfrench on Tue Apr 13, 2004 3:30 pm; edited 1 time in total
Back to top
View user's profile Send private message
Belfurious
Newbie
Newbie


Joined: 17 Dec 2003
Posts: 1
Location: Cold Spring, NY

PostPosted: Wed Dec 17, 2003 6:06 pm    Post subject: Reply with quote

That's it! dfrench, you're a genius. When I combine the two macros into one and link it to the button, I get exactly the effect I need.

Wish I could buy you a beer. Very Happy
Back to top
View user's profile Send private message Visit poster's website
elehenaff
Power User
Power User


Joined: 16 Apr 2004
Posts: 76
Location: paris - france

PostPosted: Wed May 19, 2004 1:50 am    Post subject: Reply with quote

i use this piece of code for my needs and it's really usefull.
i need to produce two kind of document from one source document.
the navigation sections are hidden and are included only in the html version not in the pdf one.
that's how i make a 'conditionnal' document!
ELH
Back to top
View user's profile Send private message Visit poster's website
vince
Guest





PostPosted: Wed May 19, 2004 3:42 am    Post subject: hyperlink Reply with quote

We can thus to reach the variables fields but how to recover the hyperlink included in a writer document ?
Back to top
dfrench
Moderator
Moderator


Joined: 03 Mar 2003
Posts: 1605
Location: Wellington, New Zealand

PostPosted: Wed May 19, 2004 11:54 am    Post subject: Reply with quote

Do a search of the forums ... you will find this one which answers the hyperlink question
http://www.oooforum.org/forum/viewtopic.php?t=8880&highlight=writer+hyperlink
Back to top
View user's profile Send private message
kerrysl
Newbie
Newbie


Joined: 25 May 2004
Posts: 4

PostPosted: Tue May 25, 2004 9:03 pm    Post subject: Reply with quote

I want to do something similar and looking for the easiest way to go about it.
This is the brief;
I have an input list that has 4 payment methods on it. One of those methods is "by EFT". If this option is selected I want to display the Bank/BSB/Account details (each on their own line).
If I give the input list a unique name, can this be referenced at all by a conditional text or macro?

What I want I guess is a combination of set variable and input list. Is this doable?
_________________
Kerry
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group