How to create an MS Word document in PowerShell and set paragraph / line spacing to zero?

I want to create a PowerShell script that will generate a Word document. It works as expected, but when I run the script, there is a space or line break between "My Document: Title" and "Date: 01-07-2014". The result is as follows:

My Document: Title

Date: 01-07-2014

I want it to look like this:

My Document: Title
Date: 01-07-2014

How can I write in this script a way to remove spaces inside paragraphs? I mean, I want to set the paragraph spacing to a single in PowerShell (and not in Word by default). By the way, if I do not add $ selection.TypeParagraph () before "Date: $ date", the result is as follows:

My Document: TitleDate: 01-07-2014

, . , , . script.

$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)
+4
2

, "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()

" "

$selection = $word.selection
$selection.WholeStory
$selection.Style = "No Spacing"

:

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

powershell?

0

All Articles