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

Beginner wonders: How to change label by macro [Solved]

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
MSPhobe
Super User
Super User


Joined: 29 Sep 2005
Posts: 529
Location: England

PostPosted: Fri Mar 17, 2006 4:05 pm    Post subject: Beginner wonders: How to change label by macro [Solved] Reply with quote

(Open Office 2.0... and apologies for the cross post... I hadn't noticed this forum before I put the following in the ooWriter forum. I won't double post often!)

I (think) I can write a macro...

I set up a Writer docoment with a button and a label field on it.

I can get clicking the button to display a message box saying "Hello world"

In an attempt to change the label field control's label property when the button is clicked, I modified the macro assigned to the button's "Mouse Button Pressed" event so that it became....

Sub SayHi
msgBox "Hello World"
laMsg.Label = "Hi"
End Sub

I must be doing some things right... I get the "Hello World" message box popping up, but then, when the system tries to process the next line of the macro, I get....

"BASIC runtime error. Object variable not set."

I've spent about an hour in the archives... I've seen more complex solutions which promise to let me change labels, but...

Can someone help me see why my uncomplicated approach is failing? Or show me the simplest way to change of the label property of the label component? (Even if the "solution" isn't very robust or generally applicable!)

Thanks... Tom


Last edited by MSPhobe on Wed Jun 13, 2007 3:19 am; edited 1 time in total
Back to top
View user's profile Send private message
JeromeC
General User
General User


Joined: 13 Oct 2005
Posts: 37
Location: France - Nantes

PostPosted: Sun Mar 19, 2006 7:05 am    Post subject: Reply with quote

Hi Tom,

I remember a problem I had few weeks ago and it seems to be yours
I don't know if it's your solution, but try that :

To set the Label property of a button, you have to write the code (oPushButton is your button) :
Code:
oPushButton.control.setPropertyValue("Label", "your text")


Maybe it can be useful....

Jerome.
Back to top
View user's profile Send private message
MSPhobe
Super User
Super User


Joined: 29 Sep 2005
Posts: 529
Location: England

PostPosted: Sun Mar 19, 2006 12:25 pm    Post subject: Reply with quote

Thank you... I think what you said was along the lines of what I wanted to do... but when I adapted it to my page, things still would not work for me.

I put the setPropertyValue call in the same macro as the one that successfully causes the msgbox "thing" to work.

In mt program, I made the line as follows...

laMsg.control.setPropertyValue("Label","hi")

... because the name of the control I wanted to re-label was laMsg (according to the object inspector, the design time properties editing thing.)

When ran the macro, I got....

"object varialbe not set"

in the setPropertyValue line.

I suspect I'm not referencing the label control properly...

Help?? Tom
Back to top
View user's profile Send private message
JeromeC
General User
General User


Joined: 13 Oct 2005
Posts: 37
Location: France - Nantes

PostPosted: Mon Mar 20, 2006 12:53 am    Post subject: Reply with quote

OK. I think I found your problem.

You can't use your control "laMsg" like that.
Each control on a document is a shape and can be used with the service com.sun.star.drawing.ControlShape

So, you have to list all drawings in your document and if your drawing is a control and if its name is "laMsg", you can set its label.

Try this code : it should work

For example :
for i=0 to Thiscomponent.drawpage.count-1 // list all drawings in your document and iterate on each drawing
oDrawPage=ThisComponent.drawpage.getByIndex(i) // set a drawing
if oDrawPage.supportsService("com.sun.star.drawing.ControlShape") then //the drawing is a control
if oDrawPage.control.Name="laMsg" then // it's your control "laMsg"
oDrawPage.control.setPropertyValue("Label","hi")
end if
end if
next


Jerome.
Back to top
View user's profile Send private message
MSPhobe
Super User
Super User


Joined: 29 Sep 2005
Posts: 529
Location: England

PostPosted: Mon Mar 20, 2006 8:05 am    Post subject: Reply with quote

Hurrah! Thank you... that did indeed work.

For others reading this thread:

I didn't have to do anything clever to my "SayHi" macro... I just plugged in Jerome's code. Didn't have to DIM i, etc.

I did have to change the "//"s to single quotes ('). to make the rems on the ends of lines be treated as rems.

For this code to work in your instance, you need to name the label control "laMsg", or change the reference to that name in Jerome's code.

===
Now... at the risk of being an Oliver ("More?").... can anyone suggest a more direct way of accomplishing the task? The overhead of the best answer to date seems excessive.

I suspect that a during-document-load "scan" of the document can assemble a lookup table of all the controls, and thus allow the programmer subsequently to go more directly to a given controlt... I can probably master that "solution" myself. But can anyone come up with a more direct route to control properties?

Thanks again, Jerome. A "seems excessive" answer that WORKS is a great step forward from a bright, elegant, idea that doesn't work!

Tom
Back to top
View user's profile Send private message
MSPhobe
Super User
Super User


Joined: 29 Sep 2005
Posts: 529
Location: England

PostPosted: Tue Jun 12, 2007 3:20 am    Post subject: Reply with quote

This saga continued at....

http://www.oooforum.org/forum/viewtopic.phtml?t=58315

... if you are trying to do what I was trying to do.
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
Page 1 of 1

 
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