Mik*_*e J 4 powershell ms-word
我期待创建一个将生成Word文档的PowerShell脚本.它按预期工作,但是当我运行脚本时,"我的文档:标题"和"日期:01-07-2014"之间存在空格或换行符.结果如下:
My Document: Title
Date: 01-07-2014
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像这样:
My Document: Title
Date: 01-07-2014
Run Code Online (Sandbox Code Playgroud)
如何在段落中删除空格的方式写入此脚本?我的意思是,我想在PowerShell中将段落间距设置为单一(默认情况下不在Word中)顺便说一下,如果我不在"Date:$ date"之前添加$ selection.TypeParagraph(),结果如下:
My Document: TitleDate: 01-07-2014
Run Code Online (Sandbox Code Playgroud)
如同,根本没有回车.目标是在回车后返回一个回车但没有空间.这是脚本.
$date = get-date -format MM-dd-yyyy
$filePath = "C:\users\myuser\file"
[ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type]
$word = New-Object -ComObject word.application
$word.visible = $true
$doc = $word.documents.add()
$selection = $word.selection
$selection.font.size = 14
$selection.font.bold = 1
$selection.typeText("My Document: Title")
$selection.TypeParagraph()
$selection.font.size = 11
$selection.typeText("Date: $date")
$doc.saveas([ref] $filePath, [ref]$saveFormat::wdFormatDocument)
Run Code Online (Sandbox Code Playgroud)
我想你想要的是将Paragraph样式从"Normal"改为"No Spacing".
代码的开头是一样的:
$date = get-date -format MM-dd-yyyy
$filePath = "C:\users\myuser\file"
[ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type]
$word = New-Object -ComObject word.application
$word.visible = $true
$doc = $word.documents.add()
Run Code Online (Sandbox Code Playgroud)
然后,您要将段落样式更改为"无间距"
$selection = $word.selection
$selection.WholeStory
$selection.Style = "No Spacing"
Run Code Online (Sandbox Code Playgroud)
然后你可以继续使用其余的代码:
$selection.font.size = 14
$selection.font.bold = 1
$selection.typeText("My Document: Title")
$selection.TypeParagraph()
$selection.font.size = 11
$selection.typeText("Date: $date")
$doc.saveas([ref] $filePath, [ref]$saveFormat::wdFormatDocument)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13688 次 |
最近记录: |