automatischer dateinamen

Programmierung unter AOO/LO (StarBasic, Python, Java, ...)

Moderator: Moderatoren

waldorio
Beiträge: 5
Registriert: Do, 01.05.2008 21:30

automatischer dateinamen

Beitrag von waldorio »

Code: Alles auswählen

    REM  *****  BASIC  *****
    'Copyright (c) 2006 Winfried Rohr, re-Solutions Software Test Engineering
    'mailto: ooo@re-solutions.de  Untere Zahlbacher Strasse 18, D-55131 Mainz

    'This program is free software; you can redistribute it and/or modify it under
    'the terms of the GNU General Public License as published by the Free Software
    'Foundation; either version 2 of the License, or (at your option) any later
    'version.

    'This program is distributed in the hope that it will be useful, but WITHOUT
    'ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    'FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

    'You should have received a copy of the GNU General Public License along with
    'this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    'Place, Suite 330, Boston, MA 02111-1307 USA
    ' ========================================================================



    ' make a copy of actual loaded document
    ' append a date string to filename
    ' and store in same location
    ' notice if already exists


    ' =====
    ' Texte fuer Nachrichten
    '
    ' Nachricht1
    CONST cDokGefunden = "Dokument ist bereits vorhanden:"
    'CONST cDokGefunden = "Document found at location"
    CONST cNoNewCopy = "Es wurde keine Kopie erzeugt!"
    'CONST cNoNewCopy = "No new copy was made!"

    'Nachricht2
    CONST cNoLoc = "Dokument noch nicht gespeichert (keine URL gefunden)"
    'CONST cNoLoc = "Document was not saved yet (could not find an URL)"
    CONST cSaveAndRerun = "Erst sichern, dann Makro eventl. erneut abrufen"
    'CONST cSaveAndRerun = "Save first, then run this macro again to make a copy"

    'Nachricht3
    CONST cRunInBase = "Keine Funktion in OOo BASE"
    'CONST cRunInBase = "No function in OOo BASE"
    CONST cDontWork = "Makro beendet"
    ' CONST cDontWork = "Macro finished"

    'Nachrichten des ErrHandlers
    CONST cErrInLine = "Fehler in Zeile: "
    'CONST cErrInLine = "Error in line: "
    CONST cErrNo = "Fehler-Nr. "
    'CONST cErrNo = "Error-No. "
    CONST cErrMod = "Fehler in Makro"
    'CONST cErrMod = "Error in macro"
    ' =====






    Sub makeDayCopy()
    sMakroName = "makeDayCopy "
    sMakroVersion = "2006-05-20"


    ' try to catch errors and display a message
    On Local Error GoTo ErrHandler



    ' =====
    ' MAIN
    ' =====

    ' get active component
    oComp = StarDesktop.CurrentComponent

    ' macro run from blank Desktop
    If oComp.supportsService(_
       "com.sun.star.frame.StartModule" ) Then   
    ' exit without action
    Exit Sub
    End If

    ' macro run from Basic IDE
    If oComp.supportsService(_
       "com.sun.star.script.BasicIDE" ) Then   
    ' exit without action
    Exit Sub
    End If

    ' macro run from Base main window
    'Nachricht3
    If oComp.supportsService(_
       "com.sun.star.sdb.OfficeDatabaseDocument" ) Then
       msgbox cRunInBase & chr(13) &_
          cDontWork , 64 , sMakroName & sMakroVersion
    Exit Sub
    End If


    oDok = ThisComponent

    'XXX warning
    'XXX code will fail on next statement, if no document is loaded
    ' check if file was stored
    If oDok.hasLocation() then
       
       ' check if HELP window is in foreground
       If Instr( oDok.getLocation() , "vnd.sun.star.help:" ) then
          '      msgbox "HELP window in foreground"

       ' exit without action
       Exit Sub
       End if

       ' yes, make a copy
       ' compose additional data to create alternative name for file
       ' here: datestamp in ISO format used (good for sorting)
       sFileURL = oDok.getURL() & "_" & cDateToISO( Now() )

       ' check if such a file exist
       if FileExists( sFileURL ) then
          ' yes there is one
          ' issue a message to inform user
          msgbox cDokGefunden & chr(13) &_
             sFileURL & chr(13) & _
             cNoCopyMade ,_
             64 ,sMakroName & sMakroVersion
       else
          ' check if we have rights to save there under this name
          ' how    ???
          ' can save with new name   

          '## note: no checking on length of filename
          '##       restricted on some platforms
          '##       untested _ not sure what will happen
          oDok.StoreToURL( sFileURL , Array() )
       end if


    Else

       ' no filename by now - cannot do a thing
       msgbox cNoLoc & chr(13) &_
          cSaveAndRerun , 64 , sMakroName & sMakroVersion

    End If


    Exit sub
    ' =====



    ' =====
    ErrHandler:
    MsgBox _
       cErrInLine & Erl & _
       CHR(10) & cErrNo & Err & ": " & Error$ ,, _
       cErrMod & sModulName & sModulVersion
    End Sub
hallo,
ich suche verzweifelt :( nach einem makro das mir auf knopfdruck aus bestehendem dokument, ein neues mit entsprechendem namen generiert.
Das einzige makro welches ich gefunden hab ist das hier oben - leider hängt es das datum hinter die dateiendung an (eben als sicherungskopie).
Schön wäre allerdings stattdessen einfach ein dateiname wie 'aktuellesdatum_uhrzeit.ods' oder noch besser 'rechnungsnummer.ods'
Kann mir jemand das obige evtl. umschreiben oder was ähnliches zeigen - ich selber hab von programmierung leider keine ahnung
besten dank
turtle47
*******
Beiträge: 1849
Registriert: Mi, 04.01.2006 20:10
Wohnort: Rheinbach

Re: automatischer dateinamen

Beitrag von turtle47 »

Hi Waldorio,

woher kommt den die Rechnungsnummer?
Mal angenommen diese steht in Zelle "A1" der "Tabelle1" dann funktioniert folgender Code:

Code: Alles auswählen

Sub ReNummer_Speichern
   dim dummy()
   oDoc = ThisComponent
   oSheet = oDoc.Sheets.getByName("Tabelle1") 'Tabellenblattname
   oCell = oSheet.getCellRangeByName("A1") 'Zelle A1, in der Rechnungsnummer steht
   sReNummer = oCell.String  
   sLaufwerk = "C:/" 'Achtung: Für den Speicherort den Laufwerkspfad anpassen
   Filename = sReNummer
   neuerpfad = sLaufwerk + Filename + ".ods"
   dateiurl=converttourl(neuerpfad)
   odoc.storeAsUrl(dateiurl,dummy())
End Sub[
Hilft das weiter?

Jürgen
Zuletzt geändert von turtle47 am Fr, 23.04.2010 23:26, insgesamt 1-mal geändert.
Software hat keinen Verstand - benutze deinen eigenen...!

Win 7 SP1/ LibreOffice 3.4.2 OOO340m1 (Build:203) / Firefox 15.0.1 / Notebook ASUS K70IO 64 Bit-Betriebssytem
waldorio
Beiträge: 5
Registriert: Do, 01.05.2008 21:30

Re: automatischer dateinamen

Beitrag von waldorio »

hi turtle,
viiiiiiiiiiielen dank - funktioniert bestens, nachdem ich bei Meldung

Basic Syntaxfehler.
Erwartet: ].

einfach die eckige klammer gelöscht hab. Immerhin hab ich jetzt durch das hin und herprobieren (glaub ich :( )
die grundstruktur der makros begriffen. Vielleicht hast du mir noch einen tipp wo ich ein gutes tutorial bezüglich
der verwendeten programmiersprache find ? Ansonsten nochmal danke und einen schönen tag.

waldorio
waldorio
Beiträge: 5
Registriert: Do, 01.05.2008 21:30

Re: automatischer dateinamen

Beitrag von waldorio »

ja prima danke,...blind am morgen. :shock:
turtle47
*******
Beiträge: 1849
Registriert: Mi, 04.01.2006 20:10
Wohnort: Rheinbach

Re: automatischer dateinamen

Beitrag von turtle47 »

Hi Waldorio,
waldorio hat geschrieben:Vielleicht hast du mir noch einen tipp wo ich ein gutes tutorial bezüglich
der verwendeten programmiersprache find ? Ansonsten nochmal danke und einen schönen tag.[/[uote]
Aber sicher doch. Schau Dir doch mal die Seite von Michael an.

Schönes Wochenende.


Jürgen
Software hat keinen Verstand - benutze deinen eigenen...!

Win 7 SP1/ LibreOffice 3.4.2 OOO340m1 (Build:203) / Firefox 15.0.1 / Notebook ASUS K70IO 64 Bit-Betriebssytem
Antworten