2

I'm trying to tell PowerShell to open a text file and choose a certain encoding option. By default, when opening this text file in Word manually, it tries to open it with Japanese encoding and so doesn't show certain characters correctly.

I've tried lots of different things but nothing works so I'm totally stuck.

This text file, amongst others, needs to be converted to PDF on a daily basis.

My current script is as follows:

$wdFormatPDF = 17

$word = New-Object -ComObject Word.Application
$word.Visible = $true

$folderpath = "C:\Users\smirabile\Downloads\report-testing-destination\*"
$fileTypes  = "accepted.spl"

Get-ChildItem -Path $folderpath -Include $fileTypes | ForEach-Object {
    $path = ($_.FullName).Substring(0, ($_.FullName).LastIndexOf("."))

    $doc = $word.Documents.Open($_.FullName)

    $Word.Selection.PageSetup.Orientation = 1
    $Word.Selection.PageSetup.LeftMargin  = 20
    $Word.Selection.PageSetup.RightMargin = 20
    $doc.Select()
    $Word.Selection.Font.Size = 9
    $doc.SaveAs([ref]$path, [ref]$wdFormatPDF)

    $doc.Close()
}

$word.Quit()
Stop-Process -Name WINWORD -Force
Maarten van Stam
  • 1,901
  • 1
  • 11
  • 16
Sal
  • 55
  • 1
  • 3
  • 8

1 Answers1

0

When in doubt, read the documentation:

Syntax

expression.Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
    PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument,
    WritePasswordTemplate, Format, Encoding, Visible, OpenConflictDocument,
    OpenAndRepair, DocumentDirection, NoEncodingDialog)

[…]
Encoding | Optional | Variant | The document encoding (code page or character set) to be used by Microsoft Word when you view the saved document. Can be any valid MsoEncoding constant. […] The default value is the system code page.

Use [Type]::Missing for parameters that you want used with their default value.

Example (using iso-8859-15 encoding):

$def = [Type]::Missing
$doc = $word.Documents.Open(
         $_.FullName, $def, $def, $def, $def, $def, $def, $def, $def, $def, 28605
       )
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • forgot to mention that I've already read this article but I'm finding it difficult to understand where or how to write the code to do this. so far I've got $Word.expression.Open($Encoding:=65001) this doesn't work and neither does so many other variants that I've tried. Sorry but I'm not very experienced with powershell so any help would be appreciated. – Sal Jan 05 '17 at 13:52
  • You cannot use named parameters. All parameters must be provided in the correct order, only a trailing list of default values can be omitted. Please take a look at the example I provided. It's there for a reason. – Ansgar Wiechers Jan 05 '17 at 13:56
  • sorry but I didn't see your example till now for some reason! although your code works can you please explain why there are multiple $def in the line? – Sal Jan 05 '17 at 14:12
  • Like I already said: *only* a *trailing* list of default values can be omitted, *all* other parameters (up to the last non-default value) *must* be provided in the correct order. The `$def` arguments fill the parameters `ConfirmConversions` through `Format` with their respective default value. – Ansgar Wiechers Jan 05 '17 at 14:17