Seite 1 von 1
Name eines geladenen Calc Dokumtes feststellen
Verfasst: Di, 11.09.2007 18:48
von gschuckar
Hallo,
ich möchte gerne ein vorhandenes Calc Dokument laden, aber nur, wenn es nicht bereits geladen ist. Zur zeit mache ich das so:
----------------
option explicit
dim oDoc as object
Sub Main
oDoc = getDocument()
End Sub
function getDocument() as Object
on Error goto ErrorHandler
Dim strLoadURL as String
Dim loadProps(0) as new com.sun.star.beans.PropertyValue
Dim oSheets as Object
' load document hidden
loadProps(0).Name = "Hidden"
'loadProps(0).Value = true ' load document hidden
loadProps(0).Value = false ' load unhide
strLoadURL = getWorkPath() & "/CalcTestDoc.ods"
getDocument = StarDesktop.loadComponentFromURL(strLoadURL,"_default",0,loadProps())
exit function
ErrorHandler:
MsgBox "Error " & Err & ": " & Error$ & " (line : " & Erl & ")" & "in main.getDocument"
end function
private function getWorkPath() as String
Dim oPathSettings as Object
oPathSettings = createUnoService("com.sun.star.util.PathSettings")
getWorkPath = oPathSettings.Work
end function
---------
Wie kann ich feststellen, ob das Dokument CalcTestDoc.ods bereits geladen ist?
Danke für einen Hilfreichen Tip
Gerd Schuckar
Re: Name eines geladenen Calc Dokumtes feststellen
Verfasst: Di, 11.09.2007 19:50
von komma4
Das wird im Allgemeinen gelöst durch Testen der geladenen Komponenten.
aus dem engl. Forum
Code: Alles auswählen
REM http://www.oooforum.org/forum/viewtopic.phtml?t=3712
REM DannyB 2003-11-06
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 )
msgbox cFile
REM If UCase( cFile ) = "C:\MYDOC.SXC" Then
If cFile = "/home/winni/findeDok.odt" 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
Damit kommst Du weiter?
Re: Name eines geladenen Calc Dokumtes feststellen
Verfasst: Di, 11.09.2007 20:19
von turtle47
Hallo,
ich habe diesen
Code etwas angepasst:
Code: Alles auswählen
Sub PruefenDateiGeoeffnet
GesuchteDatei="Deine_Datei.ods" 'Dateinamen anpassen
Dim oDesktop As Object, oDocs As Object
Dim oDoc As Object, oComponents As Object
oComponents = StarDesktop.getComponents()
oDocs = oComponents.createEnumeration()
Do While oDocs.hasMoreElements()
oDoc = oDocs.nextElement()
datei=odoc.geturl()
FileN=FileNameoutofPath(datei)
if FileN=GesuchteDatei then
x = 1
end if
Loop
if x = 1 then
msgbox "Datei ist bereits geöffnet!"
else
msgbox "Die Datei wurde nicht gefunden!"
end if
End Sub
Viel Erfolg.
Jürgen
Re: Name eines geladenen Calc Dokumtes feststellen
Verfasst: Di, 11.09.2007 20:48
von gschuckar
Hallo Winfried, hallo Jürgen
vielen Dank für die Tips. Ich mache das jetzt so:
---------------------
option explicit
dim oDoc as object
Const FILE_NAME = "CalcTestFile.ods"
Sub Main
if docIsOpen(FILE_NAME) = false then
oDoc = getDocument(FILE_NAME)
endif
End Sub
function docIsOpen(sName as String) as boolean
Dim oDesktop As Object
Dim oDocs As Object
Dim oDoc As Object
Dim oComponents As Object
Dim sDatei as String
Dim bFlag as Boolean
Dim sFileN as String
GlobalScope.BasicLibraries.LoadLibrary("Tools")
oComponents = StarDesktop.getComponents()
oDocs = oComponents.createEnumeration()
bFlag = false
Do While oDocs.hasMoreElements()
oDoc = oDocs.nextElement()
sDatei=odoc.geturl()
sFileN = FileNameOutOfPath(sDatei)
if sFileN=sName then
bFlag = true
end if
Loop
docIsOpen = bFlag
End function
---------
Funktioniert einwandfrei. Danke und schönen Abend noch
Gruß
Gerd Schuckar