| View previous topic :: View next topic |
| Author |
Message |
PatrickUlmer Guest
|
Posted: Wed Nov 05, 2003 3:21 am Post subject: get active sheet |
|
|
Hi,
how can I get the active opened sheet?
I can load a sheet or create a new over LoadComponentFromURL, but I want to edit the actual opened.
thanks...Patrick |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Wed Nov 05, 2003 6:39 am Post subject: |
|
|
Do you mean how to get the actual opened document, or the active sheet within a document?
A document (window) has one or more sheets. Do you want to access a sheet within a document? Or do you want to find a particular opened document (windows with multiple sheets)?
If a macro is running from within a Calc document, you can use ThisComponent to access the document.
You can also start at the Desktop object, get a collection of open View objects, and iterate through them looking for one that is a Calc document, and then further scrutinizing the Calc documents to see if you found the one you are interested in. For instance, write a loop that looks at all open windows and finds the one named "C:\MyBudget.sxc". _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Nov 06, 2003 5:49 am Post subject: |
|
|
| DannyB wrote: |
Do you mean how to get the actual opened document, or the active sheet within a document?
|
I want to get the actual opened document.
| DannyB wrote: |
You can also start at the Desktop object, get a collection of open View objects, and iterate through them looking for one that is a Calc document, and then further scrutinizing the Calc documents to see if you found the one you are interested in. For instance, write a loop that looks at all open windows and finds the one named "C:\MyBudget.sxc".
|
I don't know how to do that. Have you got an example?
Thanks...Patrick |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Thu Nov 06, 2003 8:41 am Post subject: |
|
|
Try this...
| Code: | Sub Main
oComponents = StarDesktop.getComponents()
' Show me how many total components are open?
nCount = 0
oComponentWalker = oComponents.createEnumeration()
Do While oComponentWalker.hasMoreElements()
oComponent = oComponentWalker.nextElement()
nCount = nCount + 1
Loop
Print "There are "; nCount; " components open."
' Walk through the components looking for documents of a specific type.
oComponentWalker = oComponents.createEnumeration()
Do While oComponentWalker.hasMoreElements()
oComponent = oComponentWalker.nextElement()
' See if component is a document.
' Any com.sun.star.document.OfficeDocument supports XModel.
' Of course, we could have just checked for the service OfficeDocument,
' see the Drawing example below for how to check for a service.
If HasUnoInterfaces( oComponent, "com.sun.star.frame.XModel" ) Then
' It will have this particular interface if it is a spreadsheet document.
If HasUnoInterfaces( oComponent, "com.sun.star.sheet.XSpreadsheetDocument" ) Then
' Notify me if we found a spreadsheet document has a sheet named "Sheet2",
' and cell C2 of that sheet contains the text "Meow Mix".
oSheets = oComponent.getSheets()
If oSheets.hasByName( "Sheet2" ) Then
oSheet = oSheets.getByName( "Sheet2" )
If oSheet.getCellByPosition( 2, 1 ).getFormula() = "Meow Mix" Then
MsgBox( "Found the componenet, which is a spreadsheet, which has a sheet names Sheet2, whose cell C2 contains Meow Mix." )
EndIf
EndIf ' If it has Sheet2
EndIf ' If it is a spreadsheet
' If the component is Untitled, that is, has not been saved, then notify me.
If Not oComponent.hasLocation() Then
MsgBox( "Untitled component found." )
EndIf ' if it is untitled.
' If the document is saved, and is C:\MYDOC.SXC
If oComponent.hasLocation() Then
cURL = oComponent.getLocation()
cFile = ConvertFromURL( cURL )
If UCase( cFile ) = "C:\MYDOC.SXC" Then
MsgBox( "Found the document C:\MYDOC.SXC" )
EndIf
EndIf
' It will have this particular SERVICE if it is a drawing document.
If oComponent.SupportsService( "com.sun.star.drawing.DrawingDocument" ) Then
' Notify me if we found a drawing document that has a shape named "Apple Jacks"
' on its second page.
oDrawPages = oComponent.getDrawPages()
if oDrawPages.getCount() >= 2 Then
oDrawPage = oDrawPages.getByIndex( 1 )
For nShape = 0 To oDrawPage.getCount() - 1
oShape = oDrawPage.getByIndex( i )
If oShape.getName() = "Apple Jacks" Then
MsgBox( "Found the component, which is a drawing, which has a shape named Apple Jacks on its second page." )
EndIf
Next
EndIf ' If it has 2 or more pages
EndIf ' If it is a drawing
EndIf ' if it has XModel (which probably means it is an OfficeDocument)
Loop
End Sub
|
This example looks at all of the open windows. It tells you if it finds certian kinds of windows with certian contents, or whether they are saved or Untitled.
I put this together simply by reading the API reference. The real skill that must be learned is how to NAVIGATE the api docs. The rest is all cake.
I think I'm going to have to write a document on how to do this. If you give a man a fish, you've fed him for a day. If you teach a man to fish, you've fed him for a lifetime.
Hope this helps. _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
Iannz OOo Advocate

Joined: 14 Feb 2004 Posts: 494 Location: Christchurch, New Zealand
|
Posted: Mon Mar 22, 2004 1:17 pm Post subject: Thanks for routine for stepping through open documents |
|
|
Thanks Danny,
I had a similar routine for stepping through the currently open documents but had forgotten about:
| Code: | | HasUnoInterfaces( oComponent, "com.sun.star.frame.XModel" ) |
so had implemented a clumsy on error routine. Your posting has helped me tidy-up a rather messy bit of coding.
I recommend the code going into Code Snippets.
Thanks again. _________________ Cheers, Ian
http://wiki.services.openoffice.org/wiki/Extensions_development_basic a wiki about writing OpenOffice.org extensions. |
|
| Back to top |
|
 |
pitonyak Administrator


Joined: 09 Mar 2004 Posts: 3618 Location: Columbus, Ohio, USA
|
Posted: Mon Mar 22, 2004 1:28 pm Post subject: |
|
|
I thought that when you loaded/opened a new document, a reference to the newly opened document was returned so that you do not have to search for it. _________________ --
Andrew Pitonyak
http://www.pitonyak.org/oo.php |
|
| Back to top |
|
 |
Iannz OOo Advocate

Joined: 14 Feb 2004 Posts: 494 Location: Christchurch, New Zealand
|
Posted: Mon Mar 22, 2004 7:20 pm Post subject: Basic function HasUnoInterface |
|
|
You are quite right Andrew "a reference to the newly opened document is returned so that you do not have to search for it", but in my case i wanted a list of all currently open Writer documents for populating a list box (similar to the bottom of the Navigator). The problem is that using ".supportsService" produces an error for components like Help and the Basic IDE. So by using the Basic function HasUnoInterface gets around this problem.
For where I have actually used it see OutlineCrossRef2.sxw downloadable from:
http://homepages.paradise.net.nz/hillview/OOo/ _________________ Cheers, Ian
http://wiki.services.openoffice.org/wiki/Extensions_development_basic a wiki about writing OpenOffice.org extensions. |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
|
|
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
|