如何在PowerShell中创建MS Word文档并将段落/行距设置为“无”?

如何在PowerShell中创建MS Word文档并将段落/行距设置为“无”?,powershell,ms-word,Powershell,Ms Word,我希望创建一个PowerShell脚本来生成Word文档。它按预期工作,但当我运行脚本时,“我的文档:标题”和“日期:01-07-2014”之间有一个空格或换行符。结果如下所示: My Document: Title Date: 01-07-2014 My Document: Title Date: 01-07-2014 My Document: TitleDate: 01-07-2014 我希望它看起来像这样: My Document: Title Date: 01-07-2014

我希望创建一个PowerShell脚本来生成Word文档。它按预期工作,但当我运行脚本时,“我的文档:标题”和“日期:01-07-2014”之间有一个空格或换行符。结果如下所示:

My Document: Title

Date: 01-07-2014
My Document: Title
Date: 01-07-2014
My Document: TitleDate: 01-07-2014
我希望它看起来像这样:

My Document: Title

Date: 01-07-2014
My Document: Title
Date: 01-07-2014
My Document: TitleDate: 01-07-2014
如何在脚本中写入删除段落内空格的方法?我的意思是,我想在PowerShell中将段落间距设置为single(默认情况下不是在Word中),如果我不在“Date:$Date”之前添加$selection.typeparation(),结果如下所示:

My Document: Title

Date: 01-07-2014
My Document: Title
Date: 01-07-2014
My Document: TitleDate: 01-07-2014
如中所示,根本没有回车。目标是让一节车厢返回,但在该车厢返回后没有空间。这是剧本

$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)

我想你想要的是将段落样式从“普通”改为“无间距”

代码的开头是相同的:

$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.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)

我不知道$selection.wholentory设置。您对$selection.style的建议立即为我纠正了这个问题。谢谢