powershell - 使用 Powershell 为 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);

https://stackoverflow.com/questions/10727919/

相关文章:

sql-server-2008 - 一个选择语句中的不同条件

php - 这个 PHP 有什么问题?什么都没有出现

asp.net-mvc-2 - 无法将类型 'double?' 隐式转换为 'double'

wpf - 从模板绑定(bind)到 View 模型的属性

php - 从 php 中的文件夹中读取文件并列出 json 文件中的条目

email - 使用 ssl 的 Yandex smtp 设置

ruby-on-rails - rails : how to check if find_or_in

php - MySQLi 查询以获取表中的最大 id?

sql-server - 从数据库中获取高于平均水平的学生

PHP Google Calendar API - 更新事件