循环遍历电子表格行,使用电子表格中的文件名将pdf转换为文本

时间:2018-03-24 12:29:02

标签: excel vba excel-vba loops pdf

我已经修改了下面的代码来循环,以便它可以将多个PDF转换为文本文件。 PDF文件名在电子表格列A中,文本文件文件名在电子表格列C中。但是,循环代码似乎不正确,因为我只能获得要转换的第一个文件,并且它会随机被卡住。不确定是否有任何善意的人可以提供一些建议?谢谢!

Sub ConvertPDF()

Dim sfile As String, dfile As String
Dim AcroXApp As Acrobat.acroApp, AcroXAVDoc As Acrobat.AcroAVDoc
Dim AcroXPDDoc As Acrobat.AcroPDDoc, jsObj As Object, Row As Long

Row = 2

sfile = Sheet1.Range("A" & Row).Value
dfile = Sheet1.Range("C" & Row).Value

Do While ws("Sheet1").Range("A" & "row") <> ""
Set AcroXApp = CreateObject("AcroExch.App")
Set AcroXAVDoc = CreateObject("AcroExch.AVDoc")
AcroXAVDoc.Open sfile, "Acrobat"
Set AcroXPDDoc = AcroXAVDoc.GetPDDoc
Set jsObj = AcroXPDDoc.GetJSObject
jsObj.SaveAs dfile, "com.adobe.acrobat.plain-text"

AcroXAVDoc.Close False
AcroXApp.Hide
AcroXApp.Exit
Loop
Row = Row + 1

End Sub

1 个答案:

答案 0 :(得分:0)

window.onclick = function(event) { if (!event.target.matches('.dropbtn')) { var dropdowns = document.getElementsByClassName("dropdown-content"); var i; for (i = 0; i < dropdowns.length; i++) { if (dropdowns[i].classList.contains('show')) { dropdowns[i].classList.remove('show'); } } } } 分配应该在Row = Row + 1语句之前,而不是之后。否则,Loop永远不会改变,为您提供您提到的行为。

同样,Rowsfile =...应位于dfile = ...之后,而不是之前。您需要DoDo之间的所有每个文件代码。

这是编辑后的版本:

Loop

Option Explicit ' At the top of the file - always use it Sub ConvertPDF() Dim sfile As String, dfile As String Dim AcroXApp As Acrobat.acroApp, AcroXAVDoc As Acrobat.AcroAVDoc Dim AcroXPDDoc As Acrobat.AcroPDDoc, jsObj As Object, row As Long Dim row As Long ' *** Added - required by Option Explicit row = 2 Do While ws("Sheet1").Range("A" & CStr(row)) <> "" ' *** Moved up; use CStr ' *** Now you are in the body of the loop, so do everything that relates to this row. sfile = Sheet1.Range("A" & CStr(row)).Value dfile = Sheet1.Range("C" & CStr(row)).Value Set AcroXApp = CreateObject("AcroExch.App") Set AcroXAVDoc = CreateObject("AcroExch.AVDoc") AcroXAVDoc.Open sfile, "Acrobat" Set AcroXPDDoc = AcroXAVDoc.GetPDDoc Set jsObj = AcroXPDDoc.GetJSObject jsObj.SaveAs dfile, "com.adobe.acrobat.plain-text" AcroXAVDoc.Close False AcroXApp.Hide AcroXApp.Exit ' *** End of the code for this row row = row + 1 ' *** Go on to the next row - moved before the Loop Loop End Sub 明确将CStr(foo)转换为字符串。为了清楚起见,我在上面添加了它。

如果您愿意,可以使用foo代替Cells进一步加快代码速度。