Error: Object reference not set to an instance of an object.

Hi,

I’m not familiar at all with Powershell but starting playing with it.

I’m currently runining a small script that converts MS Word docs into html.

The script works when I specify only the two first parms of the ‘saveAs2’ method.

But now, I want to specify another parm in 10th position of the ‘saveAs2’ method signature (encoding parm).

I red on the web that for unspecified parms I should set a type ‘Missing’. This is what I did so that my code is now like this:

function saveas-filteredhtml
{
$missing = [System.Type]::Missing
$opendoc = $word.documents.open($doc.FullName);
$opendoc.saveAs2([ref]"$htmlpath\$doc.html", [ref]$saveFormat, $missing, $missing, $missing, $missing, $missing, $missing, $missing, $missing, $missing, 65001, $missing, $missing, $missing, $missing, $missing);
$opendoc.close();
}

Unfortunatly I 'm now getting the following error when running my script :

Object reference not set to an instance of an object.
At D:\temp\Winsights\ws\test\convertdoc-tohtml_2.ps1:17 char:3
+ $opendoc.saveAs2([ref]"$htmlpath\$doc.html", [ref]$saveFormat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException

Not sure what the problem is.

Can anyone help on this ? Thanks in advance.

SibJm

 

Try

$opendoc.saveAs2([ref]"$htmlpath$doc.html", [ref]$saveFormat, [ref]$missing, [ref]$missing, [ref]$missing, [ref]$missing, [ref]$missing, [ref]$missing, [ref]$missing, [ref]$missing, [ref]$missing, [ref]65001, [ref]$missing, [ref]$missing, [ref]$missing, [ref]missing, [ref]missing)

Hi,

Thanks for your reply.

I made the change as you mention but gettign now another error :

Exception setting "SaveAs2": Cannot convert the "System.Reflection.Missing" value of type "Missing" to type "Object".
At D:\temp\Winsights\ws\test\convertdoc-tohtml_2.ps1:18 char:3
+ $opendoc.saveAs2([ref]"$htmlpath\$doc.html", [ref]$saveFormat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : RuntimeException

Hi again !

Any advice on this ? Thank

Here is my full script:

param([string]$docpath,[string]$htmlpath = $docpath)


$srcfiles = Get-ChildItem $docPath -filter "*.docx"
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML");
$word = new-object -comobject word.application
$word.Visible = $False

function saveas-filteredhtml
{

$parm = [System.Type]::Missing ;
$opendoc = $word.documents.open($doc.FullName);
$opendoc.saveAs2([ref]"$htmlpath\$doc.html", [ref]$saveFormat, [ref]$parm, [ref]$parm, [ref]$parm, [ref]$parm, [ref]$parm, [ref]$parm, [ref]$parm, [ref]$parm, [ref]$parm, 65001, [ref]$parm, [ref]$parm, [ref]$parm, [ref]$parm, [ref]$parm) ;
$opendoc.close();
}

ForEach ($doc in $srcfiles)
{
Write-Host "Processing :" $doc.FullName
saveas-filteredhtml
$doc = $null
}

$word.quit();