0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VBScriptとMS Office(Word)でPDFをdocx形式に変換する

Posted at

Microsoft WordでPDFを読み込むと、編集可能な形式に変換することが可能です。また、変換結果も再現性が高く比較的優秀ですので、コンバーターとしての利用にも有効です。

しかし、Wordにはバッチ処理に対応した変換機能はありませんので、1つ1つファイルを読み込んで変換していくことになりますが、非常に面倒ですのでVBScriptによる自動化で対応することにします。

以下のコードでは、指定されたパス(フォルダ)上に存在するPDFファイルをdocx形式に一括で変換を行います。

Option Explicit
 
 
' -----------------------------------------------------------------
' 変数定義
' -----------------------------------------------------------------
Dim oWordApp
Dim oWordDoc
Dim oFso
Dim oFolder
Dim oShell
Dim arrPath
Dim file
Dim strFile
Dim strFileBaseName
 
' -----------------------------------------------------------------
' 初期設定
' -----------------------------------------------------------------
Set oFso = WScript.CreateObject("Scripting.FileSystemObject")
Set oShell = WScript.CreateObject("WScript.Shell")
 
redim arrPath(2)
 
' PDFファイルの保存先フォルダパス
arrPath(0) = "PDFファイルがあるフォルダのパス"
arrPath(1) = "docxの出力先とするフォルダのパス"
 
' -----------------------------------------------------------------
' 処理 pdf -> docx
' -----------------------------------------------------------------
Set oWordApp = WScript.CreateObject("Word.Application")
oWordApp.Visible = true
oWordApp.DisplayAlerts = false
 
Set oFolder = oFso.getFolder(arrPath(i, 0))
for each file in oFolder.files
    ' ファイル名のセット
    strFile = cStr(file)
    strFileBaseName = oFso.getBaseName(strFile)
 
 
    ' Wordを開く
    Set oWordDoc = oWordApp.Documents.Open(strFile)
 
    ' docxで保存
    oWordDoc.SaveAs2 arrPath(i, 1) & "/" & strFileBaseName & ".docx"
 
    ' Wordを閉じる
    oWordDoc.Close
    Set oWordDoc = Nothing
next
 
oWordApp.DisplayAlerts = true
oWordApp.Quit
set oWordApp = Nothing
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?