I'm converting a "hybrid" batch/vbs script to PowerShell and converting the "batch" portion was delightfully straightforward, but I'm struggling a bit with the VBS Conversion.
I can open a word document, but when I try to pass extra params I continually get errors. I want to define each optional param, and ideally, if not passed as a param use default. I read about ISMISSING
online and something about System_Call
but I couldn't get it to work so I defined my own "default" values, but they seem all over the place.
Sometimes it was $true
other times it wants True
and sometimes I can use $wdOpenFormatAuto
other times it wants wdOpenFormatAuto
and other times it requires the enumeration. I feel like I may be just "getting it to work" and want to understand better before I start working more on this project. This is my "open" statement for MS Word as it stands. Can anybody help improve it, help me set If not Defined, use Default
for most everything.
I found this thread, which mentioned [Type]::Missing}
, and that worked for the first few params, but when I get to $PasswordDocument
it throws an error.
Open a word document and specify encoding with PowerShell
#Start Word
$ObjWord = New-Object -comobject Word.Application
$ObjWord.Visible = $OPENUI
$ObjWord.Documents.Open($FILEPATH)
#.Open
$FILENAME = $FILEPATH
IF (!$ConfirmConversions){$ConfirmConversions = $FALSE}
$ReadOnly = $TRUE
IF (!$AddToRecentFiles){$AddToRecentFiles = $FALSE}
IF (!$PasswordDocument){$PasswordDocument = 'str'}
IF (!$PasswordTemplate){$PasswordTemplate = 'str'}
$Revert = $FALSE
IF (!$WritePasswordDocument){$WritePasswordDocument = 'str'}
IF (!$WritePasswordTemplate){$WritePasswordTemplate = 'str'}
IF (!$Format){$Format = 'wdOpenFormatAuto'}
IF (!$Encoding){$Encoding = 'str'}
IF ($OPENUI -eq "FALSE"){$Visible = $FALSE}ELSE{$Visible = $TRUE}
IF (!$OpenConflictDocument){$OpenConflictDocument = $TRUE}
IF (!$OpenAndRepair){$OpenAndRepair = $TRUE}
IF (!$DocumentDirection){$DocumentDirection = $wdLeftToRight}
IF (!$NoEncodingDialog){[string]$NoEncodingDialog = 'TRUE'}
$ObjDoc=$ObjWord.documents.open($FILENAME,$ConfirmConversions,$ReadOnly,$AddToRecentFiles,$PasswordDocument,$PasswordTemplate,$Revert,$WritePasswordDocument,$WritePasswordTemplate,$Format,$Encoding,$Visible,$OpenConflictDocument,$OpenAndRepair,$DocumentDirection,$NoEncodingDialog)
Update: I was able to set "Most" to default, but it's such a game of just "trial and error" that I don't like it, I wish I understood what was going on, how to know if a value can accept missing
.
Here is where I'm at now, with errors listed by values that won't take $missing.
#.Open
$missing = [System.Type]::Missing
$str = ''
$FILENAME = $FILEPATH
IF (!$ConfirmConversions){$ConfirmConversions = $missing}
IF (!$ReadOnly){$ReadOnly = $TRUE}
IF (!$AddToRecentFiles){$AddToRecentFiles = $missing}
IF (!$PasswordDocument){$PasswordDocument = $str} #Exception setting "Open": Cannot convert the "TRUE" value of type "string" to type "Object". # Doesn't Accept $Missing
IF (!$PasswordTemplate){$PasswordTemplate = $str} #Exception setting "Open": Cannot convert the "TRUE" value of type "string" to type "Object". # Doesn't Accept $Missing
IF (!$Revert){$Revert = $False} #Parameter value was out of acceptable range # Doesn't Accept $Missing
IF (!$WritePasswordDocument){$WritePasswordDocument = $str} #Exception setting "Open": Cannot convert the "TRUE" value of type "string" to type "Object". # Doesn't Accept $Missing
IF (!$WritePasswordTemplate){$WritePasswordTemplate = $str} #Exception setting "Open": Cannot convert the "TRUE" value of type "string" to type "Object". # Doesn't Accept $Missing
IF (!$Format){$Format = 'wdOpenFormatAuto'} #Command failed # Doesn't Accept $Missing
IF (!$Encoding){$Encoding = $missing}
IF ($OPENUI -eq "FALSE"){$Visible = $FALSE}ELSE{$Visible = $TRUE}
IF (!$OpenConflictDocument){$OpenConflictDocument = $missing}
IF (!$OpenAndRepair){$OpenAndRepair = $missing}
IF (!$DocumentDirection){$DocumentDirection = $missing}
IF (!$NoEncodingDialog){[string]$NoEncodingDialog = 'TRUE'} #Object reference not set to an instance of an object. # Doesn't Accept $Missing
$ObjDoc=$ObjWord.documents.open($FILENAME,$ConfirmConversions,$ReadOnly,$AddToRecentFiles,$PasswordDocument,$PasswordTemplate,$Revert,$WritePasswordDocument,$WritePasswordTemplate,$Format,$Encoding,$Visible,$OpenConflictDocument,$OpenAndRepair,$DocumentDirection,$NoEncodingDialog)
Interestingly, I can put PasswordDocument/PasswordTemplate
& WritePasswordDocument/WritePasswordTemplate
as missing if I OMIT NoEncodingDialog
, but if I include that param, I have to set the above to a random string???
I do understand that some of these fundamentally are related, so something can't be null if another is set etc etc, but there still seems like I should be able to do this better.