Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1416to1420
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Code von Nepomuk

Code von Nepomuk
09.04.2015 09:29:56
Nepomuk
Hallo,
Nepomuk hat mal ein Code geschrieben, er soll beim öffnen der Excelmappe eine Sicherungskopie der aktuelle Arbeitsmappe erstellen und alle sich im Sicherungsordner befindliche Dateien welche älter sind als 8 Wochen löschen.
Ich wollte Ihn gerade ausprobieren und habe den gesamten code ins Modul "diese Arbeitsmappe" kopiert. Es kommt jedoch zur folgenden Fehlermeldung " PtrSafe " erwartet Sub.
Was mache ich falsch?
Kann jemand helfen?
Option Explicit
Private Declare PtrSafe Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (_
ByVal DirPath As String) As Long
Private Sub Workbook_Open()
Const COPY_FOLDER As String = "\Sicherung\"
Const STORE_DAYS As Long = 56
Dim lngReturn As Long
Dim strPath As String, strFilename As String
Dim strWorkbookName As String, strExtension As String
strPath = Path & COPY_FOLDER
lngReturn = MakeSureDirectoryPathExists(strPath)
If lngReturn = 1 Then
strWorkbookName = Left$(Name, InStrRev(Name, ".") - 1)
strExtension = Right$(Name, Len(Name) - InStrRev(Name, ".") + 1)
strFilename = strWorkbookName & "_" & Format(Now, "yyyy_mm_dd_hh_nn_ss") & strExtension
Call SaveCopyAs(strPath & strFilename)
strFilename = Dir$(strPath & strWorkbookName & "*" & strExtension)
Do Until strFilename = vbNullString
If Now - FileDateTime(strPath & strFilename) > STORE_DAYS Then _
Call Kill(strPath & strFilename)
strFilename = Dir$
Loop
Else
Call MsgBox("Ordner für Sicherungskopie konnte nich angelegt werden." & _
vbLf & vbLf & "Bitte unbedingt Herrn Kaffl Tel. 1625 verständigen.", vbCritical, "Fehler")
End If
End Sub

10
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Code von Nepumuk
09.04.2015 09:44:20
Nepumuk
Hallo,
dann hast du wohl Win in 32 Bit.
Entweder PtrSafe löschen oder
#If win64 Then
Private Declare PtrSafe Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal  _
DirPath As String) As Long
#Else
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal DirPath As  _
String) As Long
#End If
Gruß
Rudi

AW: Code von Nepomuk
09.04.2015 09:44:44
Nepomuk
Hallo,
in der ersten Programmzeile: Lib "imagehlp.dll" (_ fehlt ein Leerzeichen zwischen der Klammer und dem Unterstrich.
Gruß
Nepumuk

AW: Code von Nepomuk
09.04.2015 10:01:40
Nepomuk
Hallo Nepumuk und Rudi,
mist habe ausversehen ein neuen Beitrag geöffnet sorry.
erstmal vielen Dank für die schnelle Hilfe.
habe erstmal das Leerzeichen gesetzt und dann PtrSafe gelöscht. Es sieht jetzt so aus
wie unten. Es wurde auch eine Sicherung erstellt. Bekommt man das auch so hin das nur eine Sicherung am Tag durchgeführt wird?
Würde auch gern die Codergänzung von Rudi probieren weiss nur nicht wohin damit.
Liebe Grüsse Thomas
Option Explicit
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" ( _
ByVal DirPath As String) As Long

Private Sub Workbook_Open()
Const COPY_FOLDER As String = "\Sicherung\"
Const STORE_DAYS As Long = 56
Dim lngReturn As Long
Dim strPath As String, strFilename As String
Dim strWorkbookName As String, strExtension As String
strPath = Path & COPY_FOLDER
lngReturn = MakeSureDirectoryPathExists(strPath)
If lngReturn = 1 Then
strWorkbookName = Left$(Name, InStrRev(Name, ".") - 1)
strExtension = Right$(Name, Len(Name) - InStrRev(Name, ".") + 1)
strFilename = strWorkbookName & "_" & Format(Now, "yyyy_mm_dd_hh_nn_ss") & strExtension
Call SaveCopyAs(strPath & strFilename)
strFilename = Dir$(strPath & strWorkbookName & "*" & strExtension)
Do Until strFilename = vbNullString
If Now - FileDateTime(strPath & strFilename) > STORE_DAYS Then _
Call Kill(strPath & strFilename)
strFilename = Dir$
Loop
Else
Call MsgBox("Ordner für Sicherungskopie konnte nich angelegt werden." & _
vbLf & vbLf & "Bitte unbedingt Herrn Kaffl Tel. 1625 verständigen.", vbCritical, "Fehler")
End If
End Sub

Anzeige
AW: Code von Nepomuk
09.04.2015 10:23:14
Nepomuk
Hallo,
teste mal:
Option Explicit

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" ( _
    ByVal DirPath As String) As Long

Private Sub Workbook_Open()
    
    Const COPY_FOLDER As String = "\Sicherung\"
    Const STORE_DAYS As Long = 56
    Const BACKUP_DATE As String = "BackupDate"
    
    Dim lngReturn As Long
    Dim strPath As String, strFilename As String
    Dim strWorkbookName As String, strExtension As String
    Dim blnFound As Boolean
    Dim objDocumentProperty As DocumentProperty
    
    For Each objDocumentProperty In CustomDocumentProperties
        If objDocumentProperty.Name = BACKUP_DATE Then
            blnFound = True
            Exit For
        End If
    Next
    
    If Not blnFound Then CustomDocumentProperties.Add Name:=BACKUP_DATE, _
        LinkToContent:=False, Type:=msoPropertyTypeDate, Value:=Date - 1
    
    If CustomDocumentProperties.Item(BACKUP_DATE).Value <> Date Then
        
        CustomDocumentProperties.Item(BACKUP_DATE).Value = Date
        
        strPath = Path & COPY_FOLDER
        
        lngReturn = MakeSureDirectoryPathExists(strPath)
        
        If lngReturn = 1 Then
            
            strWorkbookName = Left$(Name, InStrRev(Name, ".") - 1)
            strExtension = Right$(Name, Len(Name) - InStrRev(Name, ".") + 1)
            
            strFilename = strWorkbookName & "_" & Format(Now, "yyyy_mm_dd_hh_nn_ss") & strExtension
            
            Call SaveCopyAs(strPath & strFilename)
            
            strFilename = Dir$(strPath & strWorkbookName & "*" & strExtension)
            
            Do Until strFilename = vbNullString
                
                If Now - FileDateTime(strPath & strFilename) > STORE_DAYS Then _
                    Call Kill(strPath & strFilename)
                
                strFilename = Dir$
                
            Loop
            
        Else
            Call MsgBox("Ordner für Sicherungskopie konnte nich angelegt werden." & _
                vbLf & vbLf & "Bitte unbedingt Herrn Kaffl Tel. 1625 verständigen.", vbCritical, "Fehler")
        End If
    End If
End Sub

Gruß
Nepumuk

Anzeige
AW: Code von Nepomuk
09.04.2015 10:34:00
Nepomuk
Hallo Nepomuk,
funktioniert ohne Fehlermeldung. Besten dank.
Das einzige was noch wäre, es wird bei jedem öffnen eine Sicherungskopie angelegt.
Geht es auch mit nur einmal täglich?
Liebe Grüsse Thomas

AW: Code von Nepomuk
09.04.2015 10:37:43
Nepomuk
Hallo,
ist doch schon eingebaut.
Gruß
Nepumuk

AW: Code von Nepomuk
09.04.2015 11:05:54
Nepomuk
Hallo Nepomuk,
mit dem neuen Code ist keine Fehlermeldung aber es wird auch keine Sicherung erstellt.
Ich habe mal im alten Code die zeile
strFilename = strWorkbookName & "_" & Format(Now, "yyyy_mm_dd") & strExtension
geändert und jetzt legt er nur eine an.
Könnte ich es so lassen?
Liebe Grüsse Thomas
Option Explicit
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" ( _
ByVal DirPath As String) As Long
Private Sub Workbook_Open()
Const COPY_FOLDER As String = "\Sicherung\"
Const STORE_DAYS As Long = 56
Dim lngReturn As Long
Dim strPath As String, strFilename As String
Dim strWorkbookName As String, strExtension As String
strPath = Path & COPY_FOLDER
lngReturn = MakeSureDirectoryPathExists(strPath)
If lngReturn = 1 Then
strWorkbookName = Left$(Name, InStrRev(Name, ".") - 1)
strExtension = Right$(Name, Len(Name) - InStrRev(Name, ".") + 1)
strFilename = strWorkbookName & "_" & Format(Now, "yyyy_mm_dd") & strExtension
Call SaveCopyAs(strPath & strFilename)
strFilename = Dir$(strPath & strWorkbookName & "*" & strExtension)
Do Until strFilename = vbNullString
If Now - FileDateTime(strPath & strFilename) > STORE_DAYS Then _
Call Kill(strPath & strFilename)
strFilename = Dir$
Loop
Else
Call MsgBox("Ordner für Sicherungskopie konnte nich angelegt werden." & _
vbLf & vbLf & "Bitte unbedingt Herrn Kaffl Tel. 1625 verständigen.", vbCritical, "Fehler")
End If
End Sub

Anzeige
AW: Code von Nepomuk
09.04.2015 11:09:05
Nepomuk
Hallo,
ich hab das getestet und es hat einwandfrei funktioniert.
Gruß
Nepumuk

AW: Code von Nepomuk
09.04.2015 11:17:40
Nepomuk
Hallo,
jetzt hab ich noch einen Fehler gefunden. Nachdem ich das Datum der Sicherungskopie geändert habe, muss ich speichern sonst wird beim 2. mal Öffnen ein weiterer Backup erstellt.
Option Explicit

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" ( _
    ByVal DirPath As String) As Long

Private Sub Workbook_Open()
    
    Const COPY_FOLDER As String = "\Sicherung\"
    Const STORE_DAYS As Long = 56
    Const BACKUP_DATE As String = "BackupDate"
    
    Dim lngReturn As Long
    Dim strPath As String, strFilename As String
    Dim strWorkbookName As String, strExtension As String
    Dim blnFound As Boolean
    Dim objDocumentProperty As DocumentProperty
    
    For Each objDocumentProperty In CustomDocumentProperties
        If objDocumentProperty.Name = BACKUP_DATE Then
            blnFound = True
            Exit For
        End If
    Next
    
    If Not blnFound Then CustomDocumentProperties.Add Name:=BACKUP_DATE, _
        LinkToContent:=False, Type:=msoPropertyTypeDate, Value:=Date - 1
    
    If CustomDocumentProperties.Item(BACKUP_DATE).Value <> Date Then
        
        CustomDocumentProperties.Item(BACKUP_DATE).Value = Date
        
        Call Save
        
        strPath = Path & COPY_FOLDER
        
        lngReturn = MakeSureDirectoryPathExists(strPath)
        
        If lngReturn = 1 Then
            
            strWorkbookName = Left$(Name, InStrRev(Name, ".") - 1)
            strExtension = Right$(Name, Len(Name) - InStrRev(Name, ".") + 1)
            
            strFilename = strWorkbookName & "_" & Format(Now, "yyyy_mm_dd_hh_nn_ss") & strExtension
            
            Call SaveCopyAs(strPath & strFilename)
            
            strFilename = Dir$(strPath & strWorkbookName & "*" & strExtension)
            
            Do Until strFilename = vbNullString
                
                If Now - FileDateTime(strPath & strFilename) > STORE_DAYS Then _
                    Call Kill(strPath & strFilename)
                
                strFilename = Dir$
                
            Loop
            
        Else
            Call MsgBox("Ordner für Sicherungskopie konnte nich angelegt werden." & _
                vbLf & vbLf & "Bitte unbedingt Herrn Kaffl Tel. 1625 verständigen.", vbCritical, "Fehler")
        End If
    End If
End Sub

Gruß
Nepumuk

Anzeige
besten Dank an Nepumuk
09.04.2015 11:31:50
Thomas
Super
jetzt geht es bei mir auch.
Vielleicht hatte ich irgendwas falsch gemacht.
Ich bedanke mich recht herzlich für deine Mühe.
liebe grüsse Thomas

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige