Last active
December 17, 2015 03:49
-
-
Save matchy256/5545798 to your computer and use it in GitHub Desktop.
指定ディレクトリのすべてのWordファイルのフッタを「ページ番号/ページ数」にする (要 win32ole)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'win32ole' | |
abort("Usage: #{$0} WordFilesDirectory") if ARGV.length < 1 | |
fso = WIN32OLE.new('Scripting.FileSystemObject') | |
wordFiles = [] | |
Dir.foreach(ARGV[0]) do |f| | |
path = File.join(ARGV[0], f) | |
if /.+\.docx?$/ =~ f && FileTest.file?(path) then | |
wordFiles << fso.GetAbsolutePathName(path) | |
end | |
end | |
abort('Unfinded Word files.') if wordFiles.empty? | |
module WORD;end | |
word = WIN32OLE.new('Word.Application') | |
WIN32OLE.const_load(word, WORD) | |
word.visible = false | |
begin | |
wordFiles.each do |f| | |
doc =word.Documents.Open(f) | |
begin | |
doc.Sections(1).Footers(1).Range.Text = '' | |
doc.ActiveWindow.ActivePane.View.SeekView = WORD::WdSeekCurrentPageFooter | |
selection = word.Selection | |
selection.ParagraphFormat.Alignment = WORD::WdAlignParagraphCenter | |
doc.Fields.Add(selection.Range, WORD::WdFieldPage) | |
selection.TypeText('/') | |
doc.Fields.Add(selection.Range, WORD::WdFieldNumPages) | |
doc.Save | |
ensure | |
doc.Close | |
end | |
end | |
ensure | |
word.Quit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment