English 中文(简体)
使用 Power Shell 添加 AUTOTEXT 到 MS 文字文档
原标题:Add AUTOTEXT to MS Word Document with Power Shell

我正在着手一个项目, 我需要在 Power Shell 生成 MS Word 文件的页眉和页脚上添加 AUTOTEXT 条目, 如 X 列表第 1 页。 我尝试从 < a href=" http://msdn. microsoft.com/ en- us/library/ mss178795%28v=vs. 80%% 29..aspx# Y570" rel=“ no follow” > 后面的 C# 示例 之后, 我试图从 < a href=" 中提取一些想法, 但我似乎无法找到如何使它发挥作用。 我很想知道是否有人可以分享一些代码来帮助我。

最佳回答

起始点。此函数将页码添加到已传递为参数的文档的页脚( 用于2010 字词) :

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 , 从 powershell 执行 :

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

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

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

$wd.quit() # exit application

macro 必须保存在 normal.dot ( normal.dotm (2010年及2010年及2010年以上) 上,才能将其保存在所有开放文档中。

这样您就可以在单词文档中自定义您想要的东西, 而不是仅将您在文档中的行为记录在宏中页眉/页脚。

问题回答

暂无回答




相关问题
What does it mean "to write a web service"?

I just asked a question about whether it was possible to write a web-page-checking code and run it from free web server, and one supporter answered and said that it was possible only if I run "a web ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Ivy, ant and start scripts

I have a project that uses ant to build and ivy for dependencies. I would like to generate the start scripts for my project, with the classpath, based on the dependencies configured in Ivy, ...

热门标签