| View previous topic :: View next topic |
| Author |
Message |
Bucky10 General User

Joined: 30 Apr 2008 Posts: 30
|
Posted: Wed Apr 30, 2008 7:37 am Post subject: using macro to enter string in a cell |
|
|
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 |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1414 Location: France
|
Posted: Wed Apr 30, 2008 8:35 am Post subject: |
|
|
Hi,
what is the question ? |
|
| Back to top |
|
 |
Bucky10 General User

Joined: 30 Apr 2008 Posts: 30
|
Posted: Wed Apr 30, 2008 11:29 am Post subject: |
|
|
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 |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8979 Location: Lexinton, Kentucky, USA
|
Posted: Wed Apr 30, 2008 11:39 am Post subject: |
|
|
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 |
|
 |
Bucky10 General User

Joined: 30 Apr 2008 Posts: 30
|
Posted: Wed Apr 30, 2008 12:32 pm Post subject: |
|
|
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 |
|
 |
Mark B Super User


Joined: 16 Feb 2007 Posts: 852 Location: Lincolnshire, UK
|
Posted: Wed Apr 30, 2008 2:50 pm Post subject: |
|
|
| 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 |
|
 |
Bucky10 General User

Joined: 30 Apr 2008 Posts: 30
|
Posted: Thu May 01, 2008 4:20 am Post subject: |
|
|
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 |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8979 Location: Lexinton, Kentucky, USA
|
Posted: Thu May 01, 2008 7:53 am Post subject: |
|
|
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 |
|
 |
Bucky10 General User

Joined: 30 Apr 2008 Posts: 30
|
Posted: Fri May 02, 2008 5:07 am Post subject: |
|
|
| Thanks for all your help! |
|
| Back to top |
|
 |
|