使用Power Shell将页眉和页脚添加到Word文档 [英] Add Headers and Footers to Word Document with Power Shell

查看:87
本文介绍了使用Power Shell将页眉和页脚添加到Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a way to insert headers and footers into a Microsoft Word document that was generated from within Power Shell. Is there a way to do this? If so, what is an example of some code needed to accomplish this?

推荐答案

# 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);

这篇关于使用Power Shell将页眉和页脚添加到Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆