提取PDF表格并插入Excel

时间:2017-09-08 20:20:35

标签: vba excel-vba pdf adobe excel

我有一个包含表格的PDF文件。我想使用Excel-VBA只搜索第一列的值数组。我现在有一个解决方案。我将PDF转换为文本文件并进行搜索。问题是有时这些值可以在多个列中找到,而我无法分辨它所属的那个。如果它在第一列中,我只想要它。

当PDF转换为文本时,它会以一种方式对其进行转换,使得每条信息都有不可预测的行数,因此我无法将其转换回基于Excel的Excel表格中的表格行数(相信我,我试过)。当前方法搜索每一行,如果它看到匹配,则检查两个字符串是否长度相同。但就像我之前提到的那样(在极少数情况下确实会发生),列中的匹配项不是我要搜索的列。所以,我想知道,有没有办法提取PDF中的一列?或者甚至是整个桌子?

Public Sub checkNPCClist()

Dim lines As String
Dim linesArr() As String
Dim line As Variant
Dim these As String

lines = Sheet2.Range("F104").Value & ", " & Sheet2.Range("F105").Value & ", " & Sheet2.Range("F106").Value & ", " & Sheet2.Range("F107").Value

linesArr() = Split(lines, ",")
For Each line In linesArr()
    If line <> " " Then
        If matchlinename(CStr(line)) = True Then these = these & Trim(CStr(line)) & ", "
    End If
Next line

If these <> "" Then
    Sheet2.Range("H104").Value = Left(these, Len(these) - 2)
Else: Sheet2.Range("H104").Value = "Nope, none."
End If

End Sub

Function matchlinename(lookfor As String) As Boolean

Dim filename As String
Dim textdata As String
Dim textrow As String
Dim fileno As Integer
Dim temp As String
fileno = FreeFile

filename = "C:\Users\...filepath"

lookfor = Trim(lookfor)

Open filename For Input As #fileno

Do While Not EOF(fileno)
    temp = textrow
    Line Input #fileno, textrow
    If InStr(1, textrow, lookfor, vbTextCompare) Then
        If Len(Trim(textrow)) = Len(lookfor) Then
            Close #fileno
            matchlinename = True
            GoTo endthis
        End If
    End If
    'Debug.Print textdata
Loop

Close #fileno
matchlinename = False
endthis:
End Function

0 个答案:

没有答案