How to specify named parameter for Word.Application Close() method

When I'm using the Word.Application to open a document I want to specify parameters for file converstion. However, when I only want to specify the READONLY, FORMAT parameters I have to specify every single parameter until I get to the 10th one.

Is there any way to pass NAMED parameters like the VBA code samples show on MSDN?

$pdf = "c:\temp\test.pdf"
$objWord = New-Object -Com Word.Application
$objWord.Visible = $true $objDocument = $objWord.Documents.Open($Pdf,$True,$True,$False,"","",$True,"","",22) 

For example, I would love if it worked like this:

$objDocument = $objWord.Documents.Open(@{filename=$pdf;ReadyOnly=$True;FileFormat=22})

http://msdn.microsoft.com/en-us/library/office/ff836084(v=office.15).as

March 21st, 2014 8:15pm

 VBA can do it because it is not COM but is API dirct.  When binding directly to a DLL we can discover and use the entry pint names and the parameter names.  OF course I do not know for sure if this is how VBA is designed.  It could just use a big lookup table.

You could create function wrappers that fill in missing argumetns.

function Call-OpenDocument{
    Param(
        $FileName,
        [switch]ReadOnly,
        [int]$FileFormat=27
        ....
       # build call
}

You can also use Invoke-Method.

Free Windows Admin Tool Kit Click here and download it now
March 21st, 2014 8:30pm

Actually I am off by one.  You can call COM by name using InvokeMethod.  This can take a dictionary of arguments.  I almost forgot that we used to do thiat in the Pre-Net world.

March 21st, 2014 8:32pm

Here are the docs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms221479(v=vs.85).aspx

Free Windows Admin Tool Kit Click here and download it now
March 21st, 2014 8:34pm

Here is a PowerShell example: http://stackoverflow.com/questions/5544844/how-to-call-a-complex-com-method-from-powershell
March 21st, 2014 8:38pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics