vba把同路径下pdf转excel并保存在同路径下的代码
时间: 2024-05-07 15:17:44 浏览: 10
以下是将同路径下的PDF文件转换为Excel并保存在同路径下的VBA代码:
```
Sub ConvertPDFToExcel()
'定义变量
Dim AcroApp As Acrobat.AcroApp
Dim AcroAVDoc As Acrobat.AcroAVDoc
Dim AcroPDDoc As Acrobat.AcroPDDoc
Dim Sheet As Worksheet
'打开PDF文件
Set AcroApp = CreateObject("AcroExch.App")
Set AcroAVDoc = CreateObject("AcroExch.AVDoc")
If AcroAVDoc.Open(ThisWorkbook.Path & "\example.pdf", "") Then
Set AcroPDDoc = AcroAVDoc.GetPDDoc
End If
'将PDF文件转换为Excel
AcroPDDoc.SaveAs ThisWorkbook.Path & "\example.xls", "com.adobe.acrobat.xls"
'关闭PDF文件
AcroAVDoc.Close True
AcroApp.Exit
'将Excel文件数据读取到工作表中
Set Sheet = ThisWorkbook.Sheets.Add
With Sheet.QueryTables.Add(Connection:= _
"TEXT;" & ThisWorkbook.Path & "\example.xls", Destination:=Range("$A$1"))
.Name = "example"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
'删除Excel文件
Kill ThisWorkbook.Path & "\example.xls"
End Sub
```
请将代码中的“example.pdf”和“example.xls”替换为你实际使用的PDF和Excel文件名。同时,需要确保安装了Adobe Acrobat软件(包括Adobe PDF Reader和Adobe PDF Writer)以及对应的Acrobat Type Library。