使用 Powershell 在 Word 中进行分栏页面布局
Columns Page Layout in Word with Powershell
我正在使用 Powershell 创建一个 word 文档,我需要创建一个类似于下面屏幕截图中显示的 GUI 方法的两列列:
我研究了其他解释基本 Powershell Word 对象、属性和方法的网站,例如这个 one。但是,在属性和方法的页面和页面中似乎还有更多功能"hidden"。我想在我的 word 文档中创建一个两列的列。这是我用来创建文档并写入的代码:
$fileName = 'C:\template.docx'
$word = New-Object -Com Word.Application
$word.Visible = $true
$document = $word.Documents.Open($fileName)
$selection = $word.Selection
$text = "Test Text."
$selection.TypeText($text)
$document.SaveAs($fileName)
$document.Close()
$word.Quit()
$word = $null
使用过 Excel ComObjects 后,要想弄清楚如何使其与 PowerShell 一起工作并不是最容易的。
你漏掉了这一行:
$selection.PageSetup.TextColumns.SetCount(2)
怎么去?
- 勾选Word Interop Com Object MSDN page
- 这可能是我们感兴趣的 PageSetup object(因为在 GUI 中,两列显示在布局 > 页面设置 > 列下)
- 谷歌搜索 "word com object pagesetup" 导致 a better MSDN documentation page that lists the properties
- 对 TextColumns - it has the methods in the "Remarks" but I prefer to a doc page which lists the members
重复此过程
- 终于找到了
SetCount
方法。
希望这能帮助您弄清楚将来如何导航 Word ComObject 文档。这些示例最多为 VBA 或 C#,需要转换为 PowerShell。
$fileName = 'C:\Template.docx'
$binding = "System.Reflection.BindingFlags" -as [type]
$word = New-Object -Com Word.Application
$word.Visible = $true
$document = $word.Documents.Open($fileName)
$selection = $word.Selection
$text = "Test Text."
$selection.TypeText($text)
$selection.PageSetup.TextColumns.SetCount(2)
# check the GUI here.
# You will see the Layout > Page Setup > Columns > Two is selected
$document.SaveAs($fileName)
$document.Close()
$word.Quit()
$word = $null
我正在使用 Powershell 创建一个 word 文档,我需要创建一个类似于下面屏幕截图中显示的 GUI 方法的两列列:
我研究了其他解释基本 Powershell Word 对象、属性和方法的网站,例如这个 one。但是,在属性和方法的页面和页面中似乎还有更多功能"hidden"。我想在我的 word 文档中创建一个两列的列。这是我用来创建文档并写入的代码:
$fileName = 'C:\template.docx'
$word = New-Object -Com Word.Application
$word.Visible = $true
$document = $word.Documents.Open($fileName)
$selection = $word.Selection
$text = "Test Text."
$selection.TypeText($text)
$document.SaveAs($fileName)
$document.Close()
$word.Quit()
$word = $null
使用过 Excel ComObjects 后,要想弄清楚如何使其与 PowerShell 一起工作并不是最容易的。
你漏掉了这一行:
$selection.PageSetup.TextColumns.SetCount(2)
怎么去?
- 勾选Word Interop Com Object MSDN page
- 这可能是我们感兴趣的 PageSetup object(因为在 GUI 中,两列显示在布局 > 页面设置 > 列下)
- 谷歌搜索 "word com object pagesetup" 导致 a better MSDN documentation page that lists the properties
- 对 TextColumns - it has the methods in the "Remarks" but I prefer to a doc page which lists the members 重复此过程
- 终于找到了
SetCount
方法。
希望这能帮助您弄清楚将来如何导航 Word ComObject 文档。这些示例最多为 VBA 或 C#,需要转换为 PowerShell。
$fileName = 'C:\Template.docx'
$binding = "System.Reflection.BindingFlags" -as [type]
$word = New-Object -Com Word.Application
$word.Visible = $true
$document = $word.Documents.Open($fileName)
$selection = $word.Selection
$text = "Test Text."
$selection.TypeText($text)
$selection.PageSetup.TextColumns.SetCount(2)
# check the GUI here.
# You will see the Layout > Page Setup > Columns > Two is selected
$document.SaveAs($fileName)
$document.Close()
$word.Quit()
$word = $null