PowerShell将域代码添加到MS

PowerShell将域代码添加到MS

本文介绍了PowerShell将域代码添加到MS Word页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某些文档中设置了域代码,据我所知,这些域代码将在打印时显示当前日期,但在其余时间中是不可见的.我现在需要将此页脚应用于数百个文档.使用PowerShell似乎应该可以做到这一点,但是我真的不知道如何使用它,也找不到很好的参考资料/文档(到目前为止,我已经获得了它来打开文档,替换文本以及关闭文档).我希望以下页脚代码位于每个页脚部分的右侧:

I have field codes set up in some documents which will display the current date when it is printed, but be invisible the rest of the time, as far as I know. I now need to apply this footer to hundreds of documents. It seems like this should be possible to do with PowerShell, but I don't really know how to use it, and can't find a good reference/documentation (So far I have gotten it to open a document, replace text, and close the document). I would like the following Field Code to be on the right side of every footer section:

{IF {PRINTDATE \ @"M/d/yyyy h:mm"} = {DATE \ @"M/d/yyyy h:mm"}自{日期\ @" M/d/yyyy}""

{ IF{PRINTDATE \@ "M/d/yyyy h:mm"}={DATE \@ "M/d/yyyy h:mm"} "UNCONTROLLED COPY AS OF { DATE \@"M/d/yyyy"}" " "

我知道这可能是一种糟糕的方法,但是,真正的问题是,如何使用PowerShell将域代码添加到Word 2007文档的页脚中?

I know that is probably a terrible way to do that, but, the real question is, how do you add field codes to footers in Word 2007 documents using PowerShell?

推荐答案

感谢tek-tips的crobin1对此问题的解答.他说他引用了 http://msdn.microsoft .com/en-us/library/bb258930%28v = office.12%29.aspx 和一堆嘿,脚本专家!"博客条目
这是一个示例脚本,几乎可以完成我想要的操作.

Thanks to crobin1 at tek-tips for the answer to this one. He said he referenced http://msdn.microsoft.com/en-us/library/bb258930%28v=office.12%29.aspx and a bunch of "Hey, Scripting Guy!" blog entries
Here is an example script that pretty much does what I wanted.

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

#Variables used
set-variable -name wdAlignPageNumberCenter -value 1

-选项常量

$fc1 = @" IF {PRINTDATE \@ "M/d/yyyy h:mm"}={DATE \@ "M/d/yyyy h:mm"} "UNCONTROLLED COPY AS OF {DATE \@ "M/d/yyyy"}" " " "@


$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() }

这篇关于PowerShell将域代码添加到MS Word页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:24