Skip to content

Instantly share code, notes, and snippets.

@lpm11
Last active December 17, 2015 20:19
Show Gist options
  • Select an option

  • Save lpm11/5667136 to your computer and use it in GitHub Desktop.

Select an option

Save lpm11/5667136 to your computer and use it in GitHub Desktop.
指定ディレクトリ (又は引数) の doc/docx を pdf に変換
Set objFs = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application")
' 1. ファイル直接引数渡しの場合
' Set args = WScript.Arguments
' 2. 指定ディレクトリ (この場合はカレントディレクトリ (".")) から探す場合
' ReDim args(-1)
' For Each filename in objFs.GetFolder(".").Files
' ext = objFs.GetExtensionName(filename)
' If (ext="doc" Or ext="docx") And Left(objFs.GetBaseName(filename),1) <> "~" Then
' ReDim Preserve args(UBound(args) + 1)
' args(UBound(args)) = filename
' End If
' Next
' 3. 引数フォルダから探す場合
' e.g.: cscript doc2pdf.vbs .
ReDim args(-1)
For Each dir in WScript.Arguments
If objFs.FolderExists(dir) Then
For Each filename in objFs.GetFolder(dir).Files
ext = objFs.GetExtensionName(filename)
If (ext="doc" Or ext="docx") And Left(objFs.GetBaseName(filename),1) <> "~" Then
ReDim Preserve args(UBound(args) + 1)
args(UBound(args)) = filename
End If
Next
End If
Next
For Each filename in args
If objFs.FileExists(filename) Then
path = objFs.GetAbsolutePathName(filename)
pdfpath = objFs.GetParentFolderName(path) & "\" & objFs.GetBaseName(path) & ".pdf"
WScript.Echo "Processing " & path & " -> " & pdfpath & "...."
Set objDoc = objWord.Documents.Open(path,,TRUE)
objWord.ActiveDocument.ExportAsFixedFormat pdfpath, 17, False
objDoc.Close(False)
End If
Next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment