0

I'm interested in how one supplies either a named argument to a ComObject function or how one supplies the default parameter without actually knowing the default value.

The task:

Using PowerShell, open a Word document in a headless mode (without the Word Window), change the document direction and save it as a PDF file.

The problem

As per the function documentation, the parameter setting the document direction is roughly the 15th optional parameter. I either need to supply the preceding 14 parameters with their default value (which I so far could't figure how to do) or, better, I'd like to use something like VB's

Documents.Open FileName:="C:\MyFiles\Test.wp", DocumentDirection:=false 

Other possibility were

$doc = $word.Documents.Open($file, $default, $default, ..., $default, wdLeftToRight)

but that is clearly the inferior solution.

Minimal source This is what I have and it works fine except for the direction part.

$file=$args[0] 
$outputFile=$args[1] 

$word = New-Object -ComObject word.application 
$doc = $word.Documents.Open $file
$doc.SaveAs([ref] $outputFile, [ref] 17)

$doc.Close()
$word.Quit() 
Pavel
  • 698
  • 1
  • 11
  • 20
  • Related post: http://stackoverflow.com/questions/5544844/how-to-call-a-complex-com-method-from-powershell – dugas Mar 01 '16 at 18:20
  • 2
    Correct. You can't specify optional parameters in PowerShell. You have to give the whole list. – Bill_Stewart Mar 01 '16 at 18:29
  • 1
    I'm pretty sure you could build something to do this with reflection. `GetMethod` returns a `MethodInfo`, which has a `GetProperty` method. It returns a `ParameterInfo` which has `DefaultValue, HasDefaultValue, and IsOptional` properties. So you could develop a PS function, say "InvokeWithNamedParameters" that emulates what VB supports. – Χpẘ Mar 02 '16 at 03:43

0 Answers0