PDA

Vollständige Version anzeigen : Existiert die Datei - VBA


Stefan Kulpa
11.01.2003, 12:14
Um zu prüfen, ob eine Datei existiert, kann man auf verschiedene Art und Weise vorgehen:

Lösung 1: Nutzung des Windows Scripting Host

Um den Windows Scripting Host benutzen zu können, muss man entweder einen Verweis auf die entsprechende DLL-Datei (SCRRUN.DLL im System-Verzeichnis) setzen, oder aber mit LateBinding (sprich: CreateObject) arbeiten.

<div><link href="http://www.ms-office-forum.de/ubb/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Function</span> FileExists(sFilePath <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">Boolean</span>
&nbsp;
<span class="REM">'// ------------------------------------------------------------------------</span>
<span class="REM">'// Variante 1 MIT Verweis auf die Microsoft Scripting Runtime.</span>
<span class="REM">'// Hier ist ein Verweis auf die SCRRUN.DLL im System-Verzeichnis notwendig.</span>
<span class="REM">'// ------------------------------------------------------------------------</span>
<span class="TOKEN">Dim</span> objFso <span class="TOKEN">As</span> <span class="TOKEN">New</span> FileSystemObject
<span class="REM">'// ------------------------------------------------------------------------</span>
<span class="REM">'// Variante 2 OHNE Verweis auf Microsoft Scripting Runtime</span>
<span class="REM">'// ------------------------------------------------------------------------</span>
<span class="TOKEN">Dim</span> objFso <span class="TOKEN">As</span> Object
<span class="TOKEN">Set</span> objFso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
<span class="REM">'// ------------------------------------------------------------------------</span>
FileExists = objFso.FileExists(sFilePath)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Sub</span> Test()
&nbsp;
<span class="TOKEN">Const</span> csMYPATH = &quot;C:\Programme\Microsoft Office\Office10\WINWORD.EXE&quot;
<span class="TOKEN">Debug.Print</span> FileExists(csMYPATH)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Sub</span></pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)

Hinweis: man muss sich für eine Variante entscheiden und den entsprechenden Teil im Beispiel deaktivieren oder löschen.

Lösung 2: String-Manipulation mit VBA-Hausmitteln

Die Lösung mit den VB-Hausmitteln basiert auf der Dir()-Funktion unter Zuhilfenahme des Error-Objekts.

<div><link href="http://www.ms-office-forum.de/ubb/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Function</span> FileExists(sFilePath <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">Boolean</span>
&nbsp;
<span class="TOKEN">If</span> Trim(sFilePath) = &quot;&quot; <span class="TOKEN">Then</span> <span class="TOKEN">Exit Function</span>
<span class="TOKEN">If</span> Right(sFilePath, 1) = &quot;\&quot; <span class="TOKEN">Then</span> <span class="TOKEN">Exit Function</span>
<span class="REM">'// ------------------------------------------------------------------------</span>
<span class="REM">'// Fehlerhandling einschalten, um VB-Meldung abzufangen</span>
<span class="REM">'// ------------------------------------------------------------------------</span>
<span class="TOKEN">On</span> <span class="TOKEN">Error</span> <span class="TOKEN">Resume</span> <span class="TOKEN">Next</span>
FileExists = Dir(sFilePath) &lt;&gt; &quot;&quot;
FileExists = FileExists <span class="TOKEN">And</span> Err.Number = 0
<span class="REM">'// ------------------------------------------------------------------------</span>
<span class="REM">'// Fehlerhandling wieder auschalten</span>
<span class="REM">'// ------------------------------------------------------------------------</span>
<span class="TOKEN">On Error GoTo 0</span>
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Sub</span> Test()
&nbsp;
<span class="TOKEN">Const</span> csMYPATH = &quot;C:\Programme\Microsoft Office\Office10\WINWORD.EXE&quot;
<span class="TOKEN">Debug.Print</span> FileExists(csMYPATH)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Sub</span></pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)

Lösung 3: API

<div><link href="http://www.ms-office-forum.de/ubb/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Const</span> MAXPATH <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = 260
&nbsp;
<span class="TOKEN">Type</span> FILETIME
dwLowDateTime <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
dwHighDateTime <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">End</span> <span class="TOKEN">Type</span>
&nbsp;
<span class="TOKEN">Type</span> WIN32_FIND_DATA
dwFileAttributes <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
ftCreationTime <span class="TOKEN">As</span> FILETIME
ftLastAccessTime <span class="TOKEN">As</span> FILETIME
ftLastWriteTime <span class="TOKEN">As</span> FILETIME
nFileSizeHigh <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
nFileSizeLow <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
dwReserved0 <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
dwReserved1 <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
cFileName <span class="TOKEN">As</span> <span class="TOKEN">String</span> * MAXPATH
cAlternate <span class="TOKEN">As</span> <span class="TOKEN">String</span> * 14
<span class="TOKEN">End</span> <span class="TOKEN">Type</span>
&nbsp;
<span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> FindFirstFile <span class="TOKEN">Lib</span> &quot;kernel32&quot; <span class="TOKEN">Alias</span> _
&quot;FindFirstFileA&quot; _
(<span class="TOKEN">ByVal</span> lpFileName <span class="TOKEN">As</span> String, _
lpFindFileData <span class="TOKEN">As</span> WIN32_FIND_DATA) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> FindClose <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
(<span class="TOKEN">ByVal</span> hFindFile <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Function</span> FileExists(<span class="TOKEN">ByVal</span> sSource <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">Boolean</span>
&nbsp;
<span class="TOKEN">Const</span> INVALID_HANDLE_VALUE <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = -1
<span class="TOKEN">Dim</span> WFD <span class="TOKEN">As</span> WIN32_FIND_DATA
<span class="TOKEN">Dim</span> lFile <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
lFile = FindFirstFile(sSource, WFD)
<span class="REM">'// Pr&uuml;fung auf g&uuml;ltigen Datei-Handle</span>
FileExists = lFile &lt;&gt; INVALID_HANDLE_VALUE
<span class="TOKEN">Call</span> FindClose(lFile)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span></pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)

FW
24.09.2006, 00:09
... mit "VBA-Hausmitteln" hilft mir diese Funktion, festzustellen, ob ein Verzeichnis oder Datei existiert:
Public Function PathExists(ByVal strPath As String) As Boolean
On Error Resume Next
GetAttr strPath
PathExists = (Err = 0)
End Function