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, 但不知怎樣改, 請教高手
我沒上線測試哦,如果有錯請自行處理一下
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
感謝~~~~
大大
這個好像不能指明資料夾是那個
你在呼叫巨集的動作是什麼?按鍵?那你只要在那個按鍵事件裡去執行這個副程式就可以
如
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