Hi specialists,
I have created a short script to generate a bunch of docx fieles I need for testing. It works quite well, but I have a problem entering a sample text with German Umlauts. the Umlauts are broken in the resulting docx. Could you guys help me with this issue?
Here is my code:
[CmdletBinding()]
param (
[Parameter()][string]$FileNamePrefix = "Test",
[Parameter()][int]$Count = 10
)
begin {
Add-Type -AssemblyName "Microsoft.Office.Interop.Word"
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$targetPath = Resolve-Path $PWD
}
process {
for ($i = 1; $i -lt $Count + 1; $i++) {
Write-Progress -Activity "Creating Word files. $i of $Count." -PercentComplete ($i/$Count*100)
$doc = $word.Documents.Add()
$doc.TextEncoding = [Microsoft.Office.Core.MsoEncoding]::msoEncodingUTF8
$selection = $word.Selection
$rtext = -join ((65..90) + (97..122) | Get-Random -Count 10 | % { [char]$_ })
$selection.TypeText("Dies ist Text $($i) mit zufälligem Text: $rtext" )
$selection.TypeParagraph()
$target = Join-Path $targetPath "$FileNamePrefix-$($i)"
$doc.SaveAs([object]$target.ToString())
$doc.Close()
}
}
end {
$word.Quit()
}
The result in Word looks like this. The “ä” is encoded wrongly:
“Dies ist Text 1 mit zufälligem Text: lugJpDqxkB”