Anzeige
Archiv - Navigation
632to636
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
632to636
632to636
Aktuelles Verzeichnis
Verzeichnis Index
Verzeichnis Index
Übersicht Verzeichnisse
Inhaltsverzeichnis

"Backup- Makro"

"Backup- Makro"
03.07.2005 12:42:19
Torben
Problem mit "Backup- Makro"
Hallo zusammen :)
Habe folgendes Makro gebastelt:
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
strSpeicherpfad As String
End Type
Declare

Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long
Declare 

Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Function GetDirectory(Msg) As String
speicherpfad = pfad
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
With bInfo
.pidlRoot = 0&
.lpszTitle = Msg
.ulFlags = &H1
End With
x = SHBrowseForFolder(bInfo)
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function



Sub speichern()
o = InputBox("Name für Backup Ordner eingeben")
If o <> "" Then
Else: o = InputBox("Name für Backup Ordner eingeben")
Do
o = InputBox("Name für Backup Ordner eingeben")
If o <> "" Then Exit Do
Loop Until o <> ""
End If
s = GetDirectory("Bitte wählen sie den Ordner aus, in dem Backup erstellt werden soll")
If s <> "" Then
ChDir s
Else: MsgBox "Ordnerauswahl erforderlich"
Do
s = GetDirectory("Bitte wählen sie den Ordner aus, in dem Backup erstellt werden soll")
If s <> "" Then Exit Do
Loop Until s <> ""
End If
If Dir(o, 16) = o Then
MsgBox "Ordner bereits vorhanden"
Else: MkDir o
End If
t = GetDirectory("Verzeichnis der zu sichernden Dateien auswählen")
u = InputBox("Dateiendung angeben,z.B. *.xls")
'With Application.FileSearch
'.NewSearch
'.LookIn = t
'.Filename = dateiendung
'.SearchSubFolders = True
'.Execute
'MsgBox .FoundFiles.Count
'End With
ChDir t
x = t & "\" & u
y = s & "\" & o
Dim index As Integer, FSYObjekt As Object, FObjekt As Object
Set FSYObjekt = CreateObject("Scripting.FileSystemObject")
With Application.FileSearch
.Filename = u
.LookIn = t
.SearchSubFolders = True
If .Execute > 0 Then
For index = 1 To .FoundFiles.Count
Set FObjekt = FSYObjekt.GetFile(.FoundFiles(index))
If .FoundFiles.Count > 0 Then FObjekt.Move "y"
Next
End If
End With
End Sub

Es funktioniert auch alles bis auf den "Schluss" wenn er die Objekte kopieren soll, da scheitert er immer...kann mir jmnd erklären wie das geht, oder ggf. Code mit Kommentare (ich möchte gerne etwas lernen ).
Danke Euch allen ! Tolle Seite !

11
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: "Backup- Makro"
03.07.2005 15:37:26
Nepumuk
Hallo Torben,
wenn du uns schreiben würdest, was dein Makro genau machen soll, dann würden wir uns leichter tun.
Gruß
Nepumuk
AW: "Backup- Makro"
03.07.2005 18:31:28
Torben
Das Makro soll den Inhalt eines Ordners, meintwegen alle *.xls Dateien in einen anderen Ordner kopieren ;)
Hoffe das ist verständlich :)
Torben
AW: "Backup- Makro"
03.07.2005 18:51:06
Nepumuk
Hi,
so etwas habe ich letzte Woche erst gemacht. Das Makro durchsucht auch alle Unterordner und erstellt die Ursprungsordner in der selben Anordnung.
Option Explicit

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" ( _
    ByVal lpFileName As String, _
    lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" ( _
    ByVal hFindFile As Long, _
    lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" ( _
    ByVal hFindFile As Long) As Long
Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" ( _
    ByVal DirPath As String) As Long
Private Declare Function MoveWindow Lib "user32.dll" ( _
    ByVal hwnd As Long, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal bRepaint As Long) As Long
Private Declare Function GetSystemMetrics Lib "user32.dll" ( _
    ByVal nIndex As Long) As Long
Private Declare Function GetWindowRect Lib "user32.dll" ( _
    ByVal hwnd As Long, _
    ByRef lpRect As RECT) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" ( _
    lpbi As InfoT) As Long
Private Declare Function CoTaskMemFree Lib "ole32" ( _
    ByVal hMem As Long) As Long
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" ( _
    ByVal lpStr1 As String, _
    ByVal lpStr2 As String) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" ( _
    ByVal pList As Long, _
    ByVal lpBuffer As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassname As String, _
    ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
    ByVal hwnd As Long, _
    ByVal Msg As Long, _
    wParam As Any, _
    lParam As Any) As Long

Private Const MAX_PATH = 260

Private Const INVALID_HANDLE_VALUE = -1

Private Const SM_CXFULLSCREEN = &H10
Private Const SM_CYFULLSCREEN = &H11

Private Const BFFM_SETSELECTION = &H466
Private Const BFFM_INITIALIZED = &H1

Private Const OUTPUTFOLDER = "G:\" 'Dein Ausgabepfad.

Private Type InfoT
    hwnd As Long
    Root As Long
    DisplayName As Long
    Title As Long
    Flags As Long
    FName As Long
    lParam As Long
    Image As Long
End Type

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type

Private Enum BIF_FLAG
    BIF_RETURNONLYFSDIRS = &H1
    BIF_DONTGOBELOWDOMAIN = &H2
    BIF_STATUSTEXT = &H4
    BIF_RETURNFSANCESTORS = &H8
    BIF_EDITBOX = &H10
    BIF_VALIDATE = &H20
    BIF_NEWDIALOGSTYLE = &H40
    BIF_BROWSEINCLUDEURLS = &H80
    BIF_BROWSEFORCOMPUTER = &H1000
    BIF_BROWSEFORPRINTER = &H2000
    BIF_BROWSEINCLUDEFILES = &H4000
    BIF_SHAREABLE = &H8000
End Enum

Private Enum FILE_ATTRIBUTE
    FILE_ATTRIBUTE_READONLY = &H1
    FILE_ATTRIBUTE_HIDDEN = &H2
    FILE_ATTRIBUTE_SYSTEM = &H4
    FILE_ATTRIBUTE_DIRECTORY = &H10
    FILE_ATTRIBUTE_ARCHIVE = &H20
    FILE_ATTRIBUTE_NORMAL = &H80
    FILE_ATTRIBUTE_TEMPORARY = &H100
End Enum

Private s_BrowseInitDir As String
Private strNewPath As String

Private Function fncGetFolder( _
        Optional ByVal sMsg As String = "Bitte wählen Sie ein Verzeichnis", _
        Optional ByVal lFlag As BIF_FLAG = BIF_RETURNONLYFSDIRS, _
        Optional ByVal sPath As String = "C:\") As String

    Dim xl As InfoT, IDList As Long, RVal As Long, FolderName As String
    s_BrowseInitDir = sPath
    With xl
        .hwnd = FindWindow("XLMAIN", Application.Caption)
        .Root = 0
        .Title = lstrcat(sMsg, "")
        .Flags = lFlag
        .FName = FuncCallback(AddressOf BrowseCallback)
    End With
    IDList = SHBrowseForFolder(xl)
    If IDList <> 0 Then
        FolderName = Space(256)
        RVal = SHGetPathFromIDList(IDList, FolderName)
        CoTaskMemFree (IDList)
        FolderName = Trim$(FolderName)
        FolderName = Left$(FolderName, Len(FolderName) - 1)
    End If
    fncGetFolder = FolderName
End Function

Private Function BrowseCallback( _
        ByVal hwnd As Long, _
        ByVal uMsg As Long, _
        ByVal wParam As Long, _
        ByVal lParam As Long) As Long

    If uMsg = BFFM_INITIALIZED Then
        Call SendMessage(hwnd, BFFM_SETSELECTION, ByVal 1&, ByVal s_BrowseInitDir)
        Call CenterDialog(hwnd)
    End If
    BrowseCallback = 0
End Function

Private Function FuncCallback(ByVal nParam As Long) As Long
    FuncCallback = nParam
End Function

Private Sub CenterDialog(hwnd As Long)
    Dim WinRect As RECT, ScrWidth As Integer, ScrHeight As Integer
    Dim DlgWidth As Integer, DlgHeight As Integer
    GetWindowRect hwnd, WinRect
    DlgWidth = WinRect.Right - WinRect.Left
    DlgHeight = WinRect.Bottom - WinRect.Top
    ScrWidth = GetSystemMetrics(SM_CXFULLSCREEN)
    ScrHeight = GetSystemMetrics(SM_CYFULLSCREEN)
    MoveWindow hwnd, (ScrWidth - DlgWidth) / 2, _
        (ScrHeight - DlgHeight) / 2, DlgWidth, DlgHeight, 1
End Sub

Private Sub prcCopy(ByVal strFolder As String)
    Dim objFSO As Object, objFile As Object
    Dim strNewFolder As String
    strNewFolder = OUTPUTFOLDER & _
        Mid$(strFolder, InStr(1, strFolder, strNewPath)) & "\"
    MakeSureDirectoryPathExists strNewFolder
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    For Each objFile In objFSO.GetFolder(strFolder).Files
        Do
            If Not (objFile.Attributes And vbSystem) And _
                objFile.Type = "Microsoft Excel-Arbeitsblatt" Then 'Excelmappen
                objFSO.CopyFile objFile, strNewFolder & objFile.Name
            End If
        Loop Until True
    Next
    Set objFSO = Nothing
End Sub

Private Sub prcFindFolder(ByVal strFolderPath As String)
    Dim WFD As WIN32_FIND_DATA, lngSearch As Long, strDirName As String
    lngSearch = FindFirstFile(strFolderPath & "\*", WFD)
    If lngSearch <> INVALID_HANDLE_VALUE Then
        Do
            If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = _
                FILE_ATTRIBUTE_DIRECTORY Then
                strDirName = Left$(WFD.cFileName, InStr(WFD.cFileName, Chr(0)) - 1)
                If (strDirName <> ".") And (strDirName <> "..") Then
                    If InStr(1, ",16,17,8208,8209,8240,8241,", "," & _
                        CStr(WFD.dwFileAttributes) & ",") <> 0 Then
                        Call prcCopy(strFolderPath & "\" & strDirName)
                        Call prcFindFolder(strFolderPath & "\" & strDirName)
                    End If
                End If
            End If
        Loop While FindNextFile(lngSearch, WFD)
        FindClose lngSearch
    End If
End Sub

Public Sub prcCopyFiles()
    Dim strFolder As String
    strFolder = Trim$(fncGetFolder(, , "F:\")) 'Vorwahlpfad
    If strFolder <> "" Then
        strNewPath = StrReverse(Mid$(StrReverse(strFolder), 1, _
            InStr(1, StrReverse(strFolder), "\") - 1))
        Call prcCopy(strFolder)
        Call prcFindFolder(strFolder)
    End If
End Sub

Gruß
Nepumuk
Anzeige
AW: "Backup- Makro"
03.07.2005 19:04:34
Nepumuk
Hi,
ich noch mal. Da habe ich noch einen, die Funktion nicht beeiträtigenden, Fehler entdeckt. Tausche das Makro "prcFindFolder" aus.
Private Sub prcFindFolder(ByVal strFolderPath As String)
    Dim WFD As WIN32_FIND_DATA, lngSearch As Long, strDirName As String
    If Right$(strFolderPath, 1) <> "\" Then strFolderPath = strFolderPath & "\"
    lngSearch = FindFirstFile(strFolderPath & "\*", WFD)
    If lngSearch <> INVALID_HANDLE_VALUE Then
        Do
            If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = _
                FILE_ATTRIBUTE_DIRECTORY Then
                strDirName = Left$(WFD.cFileName, InStr(WFD.cFileName, Chr(0)) - 1)
                If (strDirName <> ".") And (strDirName <> "..") Then
                    If InStr(1, ",16,17,8208,8209,8240,8241,", "," & _
                        CStr(WFD.dwFileAttributes) & ",") <> 0 Then
                        Call prcCopy(strFolderPath & strDirName)
                        Call prcFindFolder(strFolderPath & strDirName)
                    End If
                End If
            End If
        Loop While FindNextFile(lngSearch, WFD)
        FindClose lngSearch
    End If
End Sub

Gruß
Nepumuk
Anzeige
AW: "Backup- Makro"
03.07.2005 19:25:34
Torben
Das hilft mir aber nicht weiter, bis auf das kopieren funzt mein Makro ja auch.
Bei mir kann man zu "backupenden" Pfad und Backuppfad wählen, sowohl als auch die zu BAckupende Dateiendung....
Danke
AW: "Backup- Makro"
03.07.2005 20:17:16
Nepumuk
Hi,
war ja nur als Beispiel gedacht. Die Kopierfunktion ist die selbe.
Der entmüllte, relevante Teil deines Makros:
Dim iIndex As Integer, FSYObjekt As Object
Set FSYObjekt = CreateObject("Scripting.FileSystemObject")
With Application.FileSearch
    .Filename = u
    .LookIn = t
    .SearchSubFolders = True
    .Execute
    For iIndex = 1 To .FoundFiles.Count
        FSYObjekt.CopyFile .FoundFiles(iIndex), y
    Next
End With
Set FSYObjekt = Nothing

Index ist ein Schlüsselwort in VBA und sollte unter keinen Umständen als Variablenname verwendet werden. Wenn du die ungarische Notation verwendest, kann da nichts passieren.
http://www.it-academy.cc/content/article_browse.php?ID=995
Dadurch ist 1. dein Makro für andere leichter lesbar und du weißt 2. auch in Zeile 5.000 noch, als welcher Typ eine Variable deklariert ist. Einzelne Buchstaben als Variablenname benutze ich nicht mal zum testen. :-)
Gruß
Nepumuk
Anzeige
AW: "Backup- Makro"
03.07.2005 20:30:13
Torben
Danke erstmal ;)
ABER :)
Hier bricht er wieder ab
FSYObjekt.CopyFile .FoundFiles(iIndex), y
als zu kopierende Datein habe *.* angegeben und einen Ordner in dem Datein liegen.
Es hackt immer an der Stelle !?
Vielleicht weißt Du oder jmnd Rat ?!
AW: "Backup- Makro"
03.07.2005 20:43:44
Nepumuk
Hi,
ich hab's nur mit "*.xls" getestet. Schau mal, was in "Application.FileSearch.FoundFiles(iIndex)" für eine Datei steht. Welche Fehlermeldung kommt?
Gruß
Nepumuk
AW: "Backup- Makro"
03.07.2005 21:40:54
Torben
Bei mir gehts nicht einmal mit *.xls
Laufzeitfehler 450
Falsche Zahl an Argumenten oder ungültige zuweisung der Eigenschaft
AW: "Backup- Makro"
03.07.2005 21:52:34
Nepumuk
Hi,
meine Testanodnung arbeitet fehlerfrei:
Public Sub test()
    
    '************************************************************
    Dim u, t, y
    u = "*.xls"
    t = "D:\"
    y = "E:\testordner\" 'Ausgabeordner muss mit \ enden
    '************************************************************
    
    Dim iIndex As Integer, FSYObjekt As Object
    Set FSYObjekt = CreateObject("Scripting.FileSystemObject")
    With Application.FileSearch
        .Filename = u
        .LookIn = t
        .SearchSubFolders = True
        .Execute
        For iIndex = 1 To .FoundFiles.Count
            FSYObjekt.CopyFile .FoundFiles(iIndex), y
        Next
    End With
    Set FSYObjekt = Nothing
End Sub

Gruß
Nepumuk
Anzeige
AW: "Backup- Makro"
03.07.2005 21:59:58
Torben
ICH DANKE DIR GANZ HERZLICH !!!!

300 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige