blackmann
Goto Top

PDF auslesen und per VBA - regex.execute() in Excel schreiben

N'Abend @all, ist Freitag, doch das Thema wurmt mich:

PDF auslesen und per VBA ... von 2014 (leider nicht mehr alle Sourcen vorhanden...)


Ich bin auf der Suche für die Lösung für ein Problem (PDF --> Excel) über obigen Artikel gestolpert, begeistert ... aber trete nun ein wenig auf der Stelle.

Sub PDF2Excel()
    Dim i As Integer
    Dim strCMDLine As String, strTXT As String
    Dim FSO As Object, objSFold As Object, objWks As Object, WSHShell As Object, file As Object, rngLastRow As Range
    Dim colPFiles As New Collection, colTFiles As New Collection, regex As Object
    
    Set WSHShell = CreateObject("WScript.Shell")  
    Set FSO = CreateObject("Scripting.FileSystemObject")  
    Set regex = CreateObject("vbscript.regexp")  
    regex.MultiLine = True
    Set objSFold = FSO.GetFolder(ThisWorkbook.Path)
    
    strCMDLine = """" & ThisWorkbook.Path & "\pdftotext.exe"" -raw -layout -nopgbrk "  
        
    For Each file In objSFold.Files                                  ' alle Dateien einlesen  
        If Right(file.Path, 4) = ".pdf" Then colPFiles.Add file.Path  ' nur *.pdf  
    Next
    
    For i = 1 To colPFiles.Count
         WSHShell.Run strCMDLine & """" & colPFiles.Item(i) & """", 0, True  
    Next
    
    For Each file In objSFold.Files                                  ' wieder alles einlesen  
        If Right(file.Path, 4) = ".txt" Then colTFiles.Add file.Path  ' nur *.txt  
    Next
    
    Set objWks = Worksheets(1)

    Set rngLastRow = objWks.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

    For i = 1 To colTFiles.Count
        strTXT = FSO.OpenTextFile(colTFiles.Item(i)).ReadAll
        
        'IP auslesen  
        regex.Pattern = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"  
        Set matches = regex.Execute(strTXT)

        If matches.Count > 0 Then
            rngLastRow.Cells(5, 5).Value = matches(0).submatches(0) 
        End If       

        Set rngLastRow = rngLastRow.Offset(1, 0)
        'Textdatei löschen  
'        Kill colTFiles.Item(i)  
    Next
    Set FSO = Nothing
    Set regex = Nothing
    Set WSHShell = Nothing
    Set objSFold = Nothing
End Sub

Meine triviale Textdatei hat folgenden Inhalt:

128.158.20.46
45.185.100.58
1.1.1.1

1. Warum ist matches.count=1 ?
Alle 3 IP's werden als iO erfasst, aber als GANZES in matches abgelegt.
Erwartet hätte ich 3 einzelne, eine pro Zeile.

Aktuell wird in E6 nur eine '20' eingetragen ....

2. Was wird in matches(0).submatches(0) abgelegt und soll als Wert zurückgeschrieben werden?

3. Was bewirkt Set rngLastRow = objWks.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

Vielen Dank für Eure Tipps !!

Schönes WE wünscht BM

Content-Key: 7620818227

Url: https://administrator.de/contentid/7620818227

Printed on: May 1, 2024 at 09:05 o'clock

Mitglied: 12168552861
Solution 12168552861 Apr 12, 2024 updated at 19:36:06 (UTC)
Goto Top
1. Warum ist matches.count=1 ?
Weil das Regex-Objekt ohne folgendes immer nach dem ersten Match aufhört, Global ist per Default "false" und somit max ein Ergebnis.
regex.Global = true
2. Was wird in matches(0).submatches(0) abgelegt und soll als Wert zurückgeschrieben werden?
Der erste geklammerte Submatch.aus dem Regex.
3. Was bewirkt Set rngLastRow = objWks.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Ermittelt die nächste nicht belegte Zelle in Spalte A.

RTFM tells you everything you need 😋

Gruß
Member: Blackmann
Solution Blackmann Apr 13, 2024 at 18:10:18 (UTC)
Goto Top
Hallo @12168552861,

Vielen Dank für diesen Tipp:
regex.Global = true
Das hat dann zum Ziel geführt!

Es hat sich aber auch herausgestellt, dass eine unvollständige, nicht nachvollziehbare Szenerie in dem von mir gewähltem Beispiel mich in die Irre führte....

RTFM tells you everything you need 😋 <-- Mais bien sûr!

Schönen Samstag noch ...

BM