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

information dialogs and OOobasic

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


Joined: 27 Mar 2007
Posts: 53

PostPosted: Wed Apr 11, 2007 11:04 pm    Post subject: information dialogs and OOobasic Reply with quote

Hi,

I've constructed a macro to copy a named range from a calc document into a writer document, but now i've got the folowing problem:
when I enter a range name that doesn't exist in the calc document, the macro hangs because you get an information dialog to inform you the name entered does not exist. Does anyone now how to catch this? "on error goto" doesn't work becaus there is no error.

thanks in advance
grts
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 11, 2007 11:12 pm    Post subject: Reply with quote

Hi

You could try something like:
Code:

 on error goto RangeNotFound
 ocell = thiscomponent.sheets(0).getCellRangeByName("Fred")
 

 RangeNotFound:


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


Joined: 27 Mar 2007
Posts: 53

PostPosted: Wed Apr 11, 2007 11:23 pm    Post subject: Reply with quote

This doesn't work.
Perhaps I should say, the macro is executed from the writer file, it opens the calc file, searches the named range and copies it, then the calc document is closed and the named range is pasted on a bookmark in the writer document.
Now even with your code the problem remains: the calc file is opened, but the named range isn't found. In this case OpenOffice doesn't throw an error, but an information dialog is displayed with the text "invallid range".
I want to get rid of the dialog but I still need to know that the named range wasn't copied.

grts
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: Thu Apr 12, 2007 12:06 am    Post subject: Reply with quote

OK, let's expand this into a more useful function:
Code:

Function catch_range(Sheet as Object,rangeName as String)
Dim oCell as Object
on error goto RangeNotFound
oCell = Sheet.getCellRangeByName(rangeName)
catch_range = true
exit Function
RangeNotFound:
catch_range = false
end Function

Now you can use this to control how the macro operates:
Code:

if  catch_range(thiscomponent.Sheets(0),"Fred") then
...
else
...
end if


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


Joined: 27 Mar 2007
Posts: 53

PostPosted: Thu Apr 12, 2007 12:58 am    Post subject: Reply with quote

Thanks a lot. The code works just fine.
Back to top
View user's profile Send private message
The_Wolfpack
Power User
Power User


Joined: 27 Mar 2007
Posts: 53

PostPosted: Thu Apr 12, 2007 1:45 am    Post subject: Reply with quote

Just one more thing, could you do the same with a graph from calc?
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: Thu Apr 12, 2007 2:50 am    Post subject: Reply with quote

The_Wolfpack wrote:
Just one more thing, could you do the same with a graph from calc?

You can with another function. This checks for the title of the chart:
Code:

Function catch_chart(Sheet as Object, iTitle as String)
Dim cTitle as String
for i = 0 to Sheet.Charts.count - 1
 cTitle = Sheet.Charts(i).embeddedObject.title.string
 if cTitle = iTitle then
   catch_chart = true
   exit function
 end if
next i
catch_chart = false
end Function

If you know the name of the chart then you can use:
Code:

Function catch_chart(Sheet as Object, cName as String)
 if Sheet.Charts.hasByName(cName) then
   catch_chart = true
else
 catch_chart = false
end if
end Function

However, the charts tend to be called 'Object 1', 'Object 2' etc.

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


Joined: 27 Mar 2007
Posts: 53

PostPosted: Thu Apr 12, 2007 4:12 am    Post subject: Reply with quote

I know the name of the chart, so I was trying the second method.
However, it keeps saying the chart isn't in the document eventough I'm possitive it is. The thingis tough, I use "all" sheets to catch the chart, is this a problem or should it work. (I Don't know on what sheet the chart is going to be, the only thing I know is the name).

grts
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: Thu Apr 12, 2007 6:33 am    Post subject: Reply with quote

The naming of charts in Calc is a strange thing - even if you set the 'Name' property it doesn't change the name of the object itself. However, to find the actual names you can try:
Code:

Sub chart_names(Sheet as Object)
for i = 0 to Sheet.Charts.count - 1
msgbox Sheet.Charts(i).Name
next i
End sub

If you want to look through all the sheets then you can use something like:
Code:

Sub Main
sheet_list(thiscomponent)
end sub

Sub sheet_list (doc as object)
Dim Sheet as Object
for i = 0 to doc.sheets.count - 1
 Sheet = doc.Sheets(i)
 msgbox sheet.Name ' do whatever you need with the sheet
next i
End Sub



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


Joined: 27 Mar 2007
Posts: 53

PostPosted: Thu Apr 12, 2007 7:18 am    Post subject: Reply with quote

Ok, this is strange:

you're not able to check if a chart with a certain name is present in a document, but you are able to copy the same chart from the same document using that name.

To copy I use:
Code:
   
        dim args1(0) as new com.sun.star.beans.PropertyValue
   args1(0).Name = "ToObject"
   args1(0).Value = "testGraph"
   dim document as object
        document=oDoc.CurrentController.Frame
   dispatcher.executeDispatch(document, ".uno:GoToObject", "", 0, args1())
   dispatcher.executeDispatch(document, ".uno:Copy", "", 0, array())

This doesn't seem to make sense, does it?

You were right tough by saying the name appears not to have been changed. Do you know of any way I could fix this, cause I don't want to use the chart's title (because that could have been changed by the user).

thanks in advance
greetings
Back to top
View user's profile Send private message
The_Wolfpack
Power User
Power User


Joined: 27 Mar 2007
Posts: 53

PostPosted: Mon Apr 16, 2007 1:31 am    Post subject: Reply with quote

Hi,

I've solved my problem with the charts, now I'm using the title to check for the chart.
But while testing my app, I discovred that I can't put my named range on a random sheet in the calc document, it has to be on the first sheet. Is there anyone who knows how to check all the cheets of the calc document?

thanks in advance
grtw
Back to top
View user's profile Send private message
The_Wolfpack
Power User
Power User


Joined: 27 Mar 2007
Posts: 53

PostPosted: Mon Apr 16, 2007 5:20 am    Post subject: Reply with quote

I'm using the folowing code to check if a named range is in a calc document. This code works fine for the first two sheets, but throws a com.sun.star.uno.RuntimeException if the named range isn't on one of the first two sheets.

Code:

Function catch_range(file as Object,rangeName as String)
   Dim oCell as Object
   dim i as integer
   dim found as boolean
   found = false
    i=0
   on error goto RangeNotFound
   oCell = file.sheets(0).getCellRangeByName(rangeName)
   found = true
   catch_range=found
RangeNotFound:
if i+1< file.sheets.count-1 then
   i=i+1   
   on error goto RangeNotFound
   oCell = file.sheets(i).getCellRangeByName(rangeName)
   found= true
   catch_range=found
else
   catch_range=found
   exit function
end if
end Function


does anybody have a clue of what I could be doing wrong?

grts, thanks in advance
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