Skip to content

Instantly share code, notes, and snippets.

@ikikko
Created February 24, 2011 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikikko/841592 to your computer and use it in GitHub Desktop.
Save ikikko/841592 to your computer and use it in GitHub Desktop.
Wordのヘッダ・フッタを複数ファイル一括変更します
@Grab(group='org.codehaus.groovy.modules.scriptom', module='scriptom', version='1.6.0')
@Grab(group='org.codehaus.groovy.modules.scriptom', module='scriptom-office-2K3-tlb', version='1.6.0')
import org.codehaus.groovy.scriptom.*
import org.codehaus.groovy.scriptom.tlb.office.word.*
def docDir = 'C:/Documents and Settings/ikikko/workspace/WordFooterConverter/doc'
def headerText = '【へっだーのてきすと】'
def footerText = '【ふったーのてきすと】'
Scriptom.inApartment {
def word = new ActiveXObject("Word.Application")
try {
new File(docDir).eachFileRecurse() {
if (!it.isFile()) {
return true
}
println it.path
// オープン
def doc = word.documents.open("${it.absolutePath}")
// ヘッダ
def header = doc.Sections(1).Headers(1)
header.Range.Text = headerText
header.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight
// フッタ
def footer = doc.Sections(1).Footers(1)
footer.Range.Text = footerText
footer.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight
footer.PageNumbers.NumberStyle = WdPageNumberStyle.wdPageNumberStyleNumberInDash
footer.PageNumbers.Add(WdPageNumberAlignment.wdAlignPageNumberCenter)
// 変更履歴の非表示
// def view = doc.Windows(1).View
// view.ShowRevisionsAndComments = false
// view.RevisionsView = WdRevisionsView.wdRevisionsViewFinal
// 保存
// word.visible = true
// doc.saveAs("${it.path}}")
doc.save()
}
} finally {
word.quit()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment