wie man Excel- oder Textdateien mit VBA öffnet, weiß ich inzwischen;
wie man vorher prüfen kann, ob eine bestimmte Excel-Datei bereits geöffnet ist, ist ebenfalls klar.
Aber wie kann man mit VBA prüfen, ob eine Text-Datei (.txt, .css, .js u. ä.) bereits geöffnet ist?
wie man Excel- oder Textdateien mit VBA öffnet, weiß ich
inzwischen;
wie man vorher prüfen kann, ob eine bestimmte Excel-Datei
bereits geöffnet ist, ist ebenfalls klar.
Aber wie kann man mit VBA prüfen, ob eine Text-Datei (.txt,
.css, .js u. ä.) bereits geöffnet ist?
Du könntest alle geöffneten Workbooks durchlaufen und den Dateinamen prüfen:
Dim wb As Workbook
For Each wb In Application.Workbooks
MsgBox wb.Name
Next wb
wie man Excel- oder Textdateien mit VBA öffnet, weiß ich
inzwischen;
wie man vorher prüfen kann, ob eine bestimmte Excel-Datei
bereits geöffnet ist, ist ebenfalls klar.
Aber wie kann man mit VBA prüfen, ob eine Text-Datei (.txt,
.css, .js u. ä.) bereits geöffnet ist?
Hallo JoKu,
wenn du mit einem Editor die txt-Datei geöffnet hast funktioniert der Code nicht. Dann kann man ggfs. K.A. mit API alle prozesse überprüfen welche Dateien die offen haben.
Ist die Txt-datei mit Z.B. Excel, Word geöffnet müßte das so funktionieren:
Option Explicit
Sub YourMacro()
Dim strFileName As String
strFileName = "k:\for.txt"
If Not FileLocked(strFileName) Then
Workbooks.Open strFileName
End If
End Sub
Function FileLocked(strFileName As String) As Boolean
On Error Resume Next
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
If Err.Number 0 Then
MsgBox "Error #" & Str(Err.Number) & "-" & Err.Description
FileLocked = True
Err.Clear
End If
End Function
Überprüfen der Fenstertitel ob dort Dateiname steh
wenn du mit einem Editor die txt-Datei geöffnet hast …
ja, z. B. mit Notepad
funktioniert der Code nicht.
*seufz*
Hallo Joku,
dann brauchst du mehr Code. Klappt zumindest mit dem Editor. Kann nicht garantieren daß es für alle Prozesse klappt.
Funktion überprüft die Fenstertitel ob der reine Dateiname (ohne Pfad im Code angeben ! ) darin vorkommt.
Gruß
Reinhard
Option Explicit
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Const GW\_HWNDFIRST = 0
Const GW\_HWNDNEXT = 2
Const GWL\_STYLE = (-16)
Const WS\_VISIBLE = &H10000000
Const WS\_BORDER = &H800000
Sub YourMacro()
Dim strFileName As String
strFileName = "for.txt"
If Not FileLocked(strFileName) Then Workbooks.Open strFileName
End Sub
Function FileLocked(ByVal strFileName As String) As Boolean
Dim hWnd As Long, sTitle As String, lStyle As Long, Task\_name() As String
Dim count As Integer, index As Integer, gefunden As Boolean
hWnd = FindWindow(ByVal 0&, ByVal 0&:wink:
hWnd = GetWindow(hWnd, GW\_HWNDFIRST)
Do
gefunden = False
lStyle = GetWindowLong(hWnd, GWL\_STYLE)
lStyle = lStyle And (WS\_VISIBLE Or WS\_BORDER)
sTitle = GetWindowTitle(hWnd)
If (lStyle = (WS\_VISIBLE Or WS\_BORDER)) = True Then
If Trim(sTitle) "" Then
For index = 1 To count
If Task\_name(index) = sTitle Then
gefunden = True
Exit For
End If
Next index
If Not gefunden Then
If InStr(sTitle, strFileName) \> 0 Then
FileLocked = True
Exit Function
End If
End If
End If
End If
hWnd = GetWindow(hWnd, GW\_HWNDNEXT)
Loop Until hWnd = 0
End Function
Private Function GetWindowTitle(ByVal hWnd As Long) As String
Dim lResult As Long, sTemp As String
lResult = GetWindowTextLength(hWnd) + 1
sTemp = Space(lResult)
lResult = GetWindowText(hWnd, sTemp, lResult)
GetWindowTitle = Left(sTemp, Len(sTemp) - 1)
End Function
du hast Recht, nur beim ersten Mal ist das so, rufe nochmals
den gleichen Unsinnsnamen auf dann klappt es mit dem Fehler.
Hallo JoKu,
Hatte ich vorhin übersehen, beim ersten Mal kommt kein Fehler weil Open keinen liefert.
ab dem zweiten Mal kommt ein Fehler weil inzwischen ja Excel die Mappe „unsinniger Name“ geöffnet/erzeugt hat.
Ich habe es noch etwas umgebaut, damit es nur einmal laufen muss.
Die gefundenen offenen Fenster können dann weiter „verwendet“ werden.
Global offene\_Fenster As New Collection
Option Explicit
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Const GW\_HWNDFIRST = 0
Const GW\_HWNDNEXT = 2
Const GWL\_STYLE = (-16)
Const WS\_VISIBLE = &H10000000
Const WS\_BORDER = &H800000
Sub offeneFenster(offene\_Fenster As Collection)
'Titelzeilen der geöffneten Fenster ermitteln
Dim hWnd As Long, sTitle As String, lStyle As Long
hWnd = FindWindow(ByVal 0&, ByVal 0&:wink:
hWnd = GetWindow(hWnd, GW\_HWNDFIRST)
Do
lStyle = GetWindowLong(hWnd, GWL\_STYLE)
lStyle = lStyle And (WS\_VISIBLE Or WS\_BORDER)
sTitle = GetWindowTitle(hWnd)
If (lStyle = (WS\_VISIBLE Or WS\_BORDER)) = True Then
If Trim(sTitle) "" Then
offene\_Fenster.Add sTitle
End If
End If
hWnd = GetWindow(hWnd, GW\_HWNDNEXT)
Loop Until hWnd = 0
End Sub