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

**URL:** <https://forums.powershell.org/t/error-object-reference-not-set-to-an-instance-of-an-object/12062>\
**Category:** PowerShell Help\
**Created:** [February 19, 2019, 10:02am UTC](https://forums.powershell.org/t/error-object-reference-not-set-to-an-instance-of-an-object/12062 "2019-02-19T10:02:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sib](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sib](https://forums.powershell.org/u/sib)\
**Post date:** [February 19, 2019, 10:02am UTC](https://forums.powershell.org/t/error-object-reference-not-set-to-an-instance-of-an-object/12062/1 "2019-02-19T10:02:57Z")

</div>

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

&nbsp;

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [February 19, 2019, 10:43am UTC](https://forums.powershell.org/t/error-object-reference-not-set-to-an-instance-of-an-object/12062/2 "2019-02-19T10:43:20Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![sib](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sib](https://forums.powershell.org/u/sib)\
**Post date:** [February 20, 2019, 2:45am UTC](https://forums.powershell.org/t/error-object-reference-not-set-to-an-instance-of-an-object/12062/3 "2019-02-20T02:45:50Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![sib](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sib](https://forums.powershell.org/u/sib)\
**Post date:** [February 21, 2019, 5:49am UTC](https://forums.powershell.org/t/error-object-reference-not-set-to-an-instance-of-an-object/12062/4 "2019-02-21T05:49:54Z")

</div>

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();
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/error-object-reference-not-set-to-an-instance-of-an-object/12062/5 "2024-05-16T20:33:15Z")

</div>

Closed on May 16, 2024, 8:33pm UTC


