Norman_SK
Norman_SK

Reputation: 35

Output a New Line Character in a String to Word with Powershell

I am authoring a docx file in Powershell and I am outputting using TypeText() to a Selection from a Word.Application object and TypeParagraph() for a new line and this is working fine.

My question is how do I do a new line mid string?

I will need both return and shift + return.

Thanks in advance, Norman

EDIT: Here is a basic script.

$objWord = New-Object -comobject Word.Application  
$objWord.Visible = $True 

$objDoc = $objWord.Documents.Open("C:\Test\Template.docx")
$objSelection = $objWord.Selection
$a = $objSelection.EndKey(6, 0)

$text = "Shift + enter that works`vThanks Dangph"
$objSelection.TypeText($text)
$objSelection.TypeParagraph()
$text = "Enter that`r`nworks"
$objSelection.TypeText($text)
$objSelection.TypeParagraph()

While knocking the above up I have realised that the issue with `r`n that I had is actually an issue with Selection.Find.Execute. I will raise this as a separate question.

So in short `v for shift + enter and `r`n for enter.

Upvotes: 1

Views: 5041

Answers (1)

dan-gph
dan-gph

Reputation: 16909

It seems that a line break is done in Word with a vertical tab, which is represented in PowerShell as:

"`v"

Try doing a TypeText with this:

"hello`vthere" 

(Make sure to use double quotes and not single quotes.)

Upvotes: 2

Related Questions