iT邦幫忙

1

關於pdf 轉換.txt 的資料夾轉換一問

Option Explicit
Sub totxt_Click()
Dim AcroXApp As Acrobat.AcroApp
Dim AcroXAVDoc As Acrobat.AcroAVDoc
Dim AcroXPDDoc As Acrobat.AcroPDDoc
Dim Filename As String
Dim jsObj As Object
Dim NewFileName As String


Filename = "C:\Users\Tom\Desktop\analysis\123.pdf"
NewFileName = "C:\Users\Tom\Desktop\analysis\123.txt"

Set AcroXApp = CreateObject("AcroExch.App")
'AcroXApp.Show

Set AcroXAVDoc = CreateObject("AcroExch.AVDoc")
AcroXAVDoc.Open Filename, "Acrobat"

Set AcroXPDDoc = AcroXAVDoc.GetPDDoc


Set jsObj = AcroXPDDoc.GetJSObject


jsObj.SaveAs NewFileName, "com.adobe.acrobat.plain-text"


AcroXAVDoc.Close False
AcroXApp.Hide
AcroXApp.Exit

End Sub

以上便是轉換txt 的code, 想用一資料夾內轉換整批pdf, 但不知怎樣改, 請教高手

1 個回答

1
japhenchen
iT邦大師 1 級 ‧ 2021-02-18 14:55:30
最佳解答

我沒上線測試哦,如果有錯請自行處理一下

ConvertAllPDFtoTXTInFolder 轉換資料夾裡的PDF成TXT

Sub ConvertAllPDFtoTXTInFolder(foldername)

Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder(foldername)
For Each oFile In oFolder.Files
    if ucase(right(oFile.Name,4)) = '.PDF' Then
        PDF2TXT oFile.Name
    end if
    i = i + 1
Next oFile
End Sub



'------------ 以下是你的程式改寫成sub
Sub PDF2TXT(Filename)
Dim AcroXApp As Acrobat.AcroApp
Dim AcroXAVDoc As Acrobat.AcroAVDoc
Dim AcroXPDDoc As Acrobat.AcroPDDoc
Dim Filename As String
Dim jsObj As Object
Dim NewFileName As String

'Filename變成這個sub的參數了
NewFileName =Replace(Filename,".pdf",".txt", , , vbTextCompare)
' vbTextCompare 忽視大小寫,把檔名.pdf變.txt

Set AcroXApp = CreateObject("AcroExch.App")
'AcroXApp.Show

Set AcroXAVDoc = CreateObject("AcroExch.AVDoc")
AcroXAVDoc.Open Filename, "Acrobat"

Set AcroXPDDoc = AcroXAVDoc.GetPDDoc


Set jsObj = AcroXPDDoc.GetJSObject


jsObj.SaveAs NewFileName, "com.adobe.acrobat.plain-text"


AcroXAVDoc.Close False
AcroXApp.Hide
AcroXApp.Exit

End Sub

cyris iT邦新手 5 級 ‧ 2021-02-18 15:23:14 檢舉

感謝~~~~

cyris iT邦新手 5 級 ‧ 2021-02-18 17:33:27 檢舉

大大
這個好像不能指明資料夾是那個

你在呼叫巨集的動作是什麼?按鍵?那你只要在那個按鍵事件裡去執行這個副程式就可以


Sub Button_Click()
  ConvertAllPDFtoTXTInFolder "C:\PDF"        
End Sub

' --- 以下是我給的程式
Sub ConvertAllPDFtoTXTInFolder(foldername)

Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder(foldername)
For Each oFile In oFolder.Files
    if ucase(right(oFile.Name,4)) = '.PDF' Then
        PDF2TXT oFile.Name
    end if
    i = i + 1
Next oFile
End Sub

我要發表回答

立即登入回答