A
A
Alexander2016-02-05 10:10:33
PowerShell
Alexander, 2016-02-05 10:10:33

How to specify encodings when saving Word file in Powershell?

Hello,
I am creating a document using a PowerShell script through the Word.Application COM object and I can’t figure out how to specify the encoding in the .SaveAs() method. I write like this:

...
$SaveFormatTXT = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “wdFormatText”)
$Encoding = [Enum]::Parse([Microsoft.Office.Core.MsoEncoding], "msoEncodingUTF8")
...
$MSWord = New-Object -ComObject Word.Application
$MSWord.Documents.add()
$Selection = $MSWord.Selection
$Selection.TypeText("бла бла бла")
$MSWord.ActiveDocument.SaveAs([ref]$($LocalSignatureFile + ".txt"),[ref]$SaveFormatTXT,[ref]$Encoding)

And I get an error:
"Exception calling "SaveAs" with "3" argument(s): "Parameter value is out of range"
Without specifying $Encoding, the document is saved, but all Cyrillic is visible as "????".
How to use this method?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question