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

using macro to enter string in a cell

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Calc
View previous topic :: View next topic  
Author Message
Bucky10
General User
General User


Joined: 30 Apr 2008
Posts: 30

PostPosted: Wed Apr 30, 2008 7:37 am    Post subject: using macro to enter string in a cell Reply with quote

This is my first attempt at writing an oo macro.
If cell B8 has value > 0, then I want to enter "bv" in cell D8

So far I have:
sub bvkneewall
dim oSheets
dim oSheet
dim oCell

oSheets = ThisComponent.getSheets()
oSheet = oSheets.getByIndex(1)
oCell = oSheet.getCellRangeByName("B8")

if oCell.getValue() > 0 Then
oCell = oSheet.getCellRangeByName("D8")
oCell.setString("bv")
end if

end sub

Thanks for your help!
Back to top
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Wed Apr 30, 2008 8:35 am    Post subject: Reply with quote

Hi,
what is the question ?
Back to top
View user's profile Send private message Visit poster's website
Bucky10
General User
General User


Joined: 30 Apr 2008
Posts: 30

PostPosted: Wed Apr 30, 2008 11:29 am    Post subject: Reply with quote

Silly me
the question is "Why doesn't it work?"
I have another macro in this spreadsheet that works, but it was created with the recorder. When I run this macro, there is no error message yet cell B8 is not getting the value "bv"

Thanks
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 Apr 30, 2008 11:39 am    Post subject: Reply with quote

If cell B8 is located on Sheet1 then you need to change 1 to 0 in this line because sheets are indexed from 0:
oSheet = oSheets.getByIndex(1)
Back to top
View user's profile Send private message
Bucky10
General User
General User


Joined: 30 Apr 2008
Posts: 30

PostPosted: Wed Apr 30, 2008 12:32 pm    Post subject: Reply with quote

Eureka!
May I also ask...
I've worked with macros & scripting in several apps, but this oo stuff like
dim oSheets
oSheets = ThisComponent.getSheets()
was confusing. I copied examples I found and crossed my fingers.

Have I defined things properly at the top? What is the bare minimum to define? I have looked at the documentation on the oo site but have not found anything that clues me in to the basic macro structure. Perhaps you could point me to an online reference?

Many thanks!
Back to top
View user's profile Send private message
Mark B
Super User
Super User


Joined: 16 Feb 2007
Posts: 852
Location: Lincolnshire, UK

PostPosted: Wed Apr 30, 2008 2:50 pm    Post subject: Reply with quote

Bucky10 wrote:
What is the bare minimum to define?


Personally instead of :
Code:

dim oSheets
dim oSheet
dim oCell

oSheets = ThisComponent.getSheets()
oSheet = oSheets.getByIndex(1)


I'd use:
Code:

dim oSheet
dim oCell

oSheet = ThisComponent.Sheets(1)



or even
Code:


dim oCell

oCell = ThisComponent.Sheets(1).getCellRangeByName("D8")




Mark
_________________
Mark B's Articles
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
Bucky10
General User
General User


Joined: 30 Apr 2008
Posts: 30

PostPosted: Thu May 01, 2008 4:20 am    Post subject: Reply with quote

Thanks,
that helps me understand a bit better
I've tried to find some resources to educate myself, but it appears there are 2 ways to structure the macro?
I'm talking about stuff like this:
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Text"
args1(0).Value = "Andrew Pitonyak"
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0,

Is this the new way? Seems like a lot of typing!
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


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

PostPosted: Thu May 01, 2008 7:53 am    Post subject: Reply with quote

The code you show above utilizes the "dispatcher" and is generally generated by recording a macro. However, what you can record is very limited and, personally, I avoid the dispatcher if I can figure out how to write the code myself.

If you are going to code and don't have the Xray utility yet you really need to get it.
http://ooomacros.org/dev.php#101416
Back to top
View user's profile Send private message
Bucky10
General User
General User


Joined: 30 Apr 2008
Posts: 30

PostPosted: Fri May 02, 2008 5:07 am    Post subject: Reply with quote

Thanks for all your help!
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 Calc 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