1

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.

FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57
  • Check the "Null conditional assignment operator ??=" from https://learn.microsoft.com/en-gb/powershell/scripting/whats-new/what-s-new-in-powershell-70?view=powershell-7#null-coalescing-assignment-and-conditional-operators. – EylM Aug 24 '20 at 16:30
  • I can usually read code and understand it, but that's confusing! I'll have to test it out, but i should have added values can't be set to null – FreeSoftwareServers Aug 24 '20 at 16:40
  • @FreeSoftwareServers Where do all of these variables come from in the first place? – Mathias R. Jessen Aug 24 '20 at 17:40
  • @MathiasR.Jessen I'm not sure what you mean, but the variables are defined here --> https://learn.microsoft.com/en-us/office/vba/api/Word.Documents.Open – FreeSoftwareServers Aug 24 '20 at 17:57
  • No, i meant why are you doing `IF(!$Format){$Format = 'wdOpenFormatAuto'}` and not just `$Format = 'wdOpenFormatAuto'`? There's no automatic variable called `$Format`, and there's no previous mention of it in the code you've posted - so where do you expect `$Format` to come from? – Mathias R. Jessen Aug 24 '20 at 18:19
  • @MathiasR.Jessen I want any variable to be optionally set if desired, this way it allows the user to set format if desired, but by default its not set – FreeSoftwareServers Aug 24 '20 at 20:52
  • @EylM what am I supposed to glean from that just curious? How does that fit into my issue/script? – FreeSoftwareServers Aug 24 '20 at 22:24

0 Answers0