powershell - 使用 Powershell 为 Word 文档添加页眉和页脚

标签 powershell scripting ms-word

我正在寻找一种方法,将页眉和页脚插入到从 Power Shell 生成的 Microsoft Word 文档中。有没有办法做到这一点?如果是这样,完成此操作所需的一些代码示例是什么?

最佳答案

# Create a new Word application COM object
$Word = New-Object -ComObject Word.Application;
# Make the Word application visible
$Word.Visible = $true;
# Add a new document to the application
$Doc = $Word.Documents.Add();
# Get the first Section of the Document object
$Section = $Doc.Sections.Item(1);
# Get the header from the Section object
$Header = $Section.Headers.Item(1);
# Get the footer from the Section object
$Footer = $Section.Footers.Item(1);

# Set the text for the header and footer
$Header.Range.Text = "Hey, I'm the header!";
$Footer.Range.Text = "Hey, I'm the footer!";

# Create a Table of Contents (ToC)
$Toc = $Doc.TablesOfContents.Add($Section.Range);

关于powershell - 使用 Powershell 为 Word 文档添加页眉和页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10727919/

相关文章:

powershell - 如何在没有换行符的情况下写入标准输出?

powershell - 如何在 PowerShell 中将字符串转换为整数

windows - 在 Windows 上自动插入字符串

linux - 搜索某些模式并替换行中间的某些文本并忽略该行的最后一个字符

linux - 当由 cron 运行时,shell 脚本中使用的环境变量在日志文件中显示为空白

c# - 是否可以在 IE 和 Word 附加组件之间共享 ASP.NET session cookie

powershell - 使用终端 Sublime Text 3 以管理员身份运行 Powershell

wpf - 自定义菜单WPF(XAML)

vba - 创建和评分测验的建议

syntax - Pandoc,Markdown to Doc,如何使用变量?