2012-05-25 4 views
0

파워 쉘의 헤더와 푸터에 MS 워드 문서를 생성하기 위해 X 목록 1 페이지와 같은 AUTOTEXT 항목을 추가해야하는 프로젝트에서 작업하고 있습니다. following C# examples에서 아이디어를 추출하려고했지만 어떻게 작동시키는 지 알 수 없습니다. 나는 누군가가 이것을 도와 줄 수있는 코드를 공유 할 수 있는지 궁금했다.파워 쉘 MS Word 문서에 AUTOTEXT 추가

답변

1

출발점.

$wd = new-object -comobject word.application # create a com object interface (word application) 

$wd.documents.open("C:\word\test.doc") # open doc 

$wd.run("Macro01") # exec macro named macro01 that add custom footer and/or header 

$wd.quit() # exit application 

macro해야 :

function Add-PageFooter ([string]$Document) { 

add-type -AssemblyName "Microsoft.Office.Interop.Word" 

set-variable -name wdAlignPageNumberCenter -value 1 -option constant 

$fc1 = "Page" 

$word = New-Object -comobject Word.Application 
$Word.Visible = $True 
#$Word.Visible = $False 

$fc2 = [ref] "" -as [Type] 

$OpenDoc = $Word.Documents.Open($Document) 
$c = $OpenDoc.Sections.Item(1).Footers.Item(1).PageNumbers.Add($wdAlignPageNumberCenter) 
$range1 = $openDoc.Sections.Item(1).Footers.Item(1).range 
$field1 = $OpenDoc.Fields.Add($range1, -1, $fc2) 
$field1.Code.Text = $fc1 
$field1.Update 

#$OpenDoc.Close() 
} 

또 다른 방법은 워드 Macro를 작성하고 파워 쉘에서 실행하는 것입니다 :이 기능은 매개 변수로 전달 된 문서의 바닥 글에 페이지 번호를 추가 (워드 2010 사용) 열려있는 모든 문서에 normal.dot (2010 년 이상은 normal.dotm)으로 저장해야합니다.

이렇게하면 워드 문서에서 원하는 내용을 사용자 정의 할 수 있으며 매크로의 머리글/바닥 글 녹음은 문서의 작업을 사용자 정의 할 수 있습니다.