I’m working on a project where I need to convert a few thousand documents from HTML to Word. I’m working with this function:
function saveas-document ($docs) {
$opendoc = $word.documents.open($docs);
$savepath = $docs -replace [regex]::escape($htmPath),"$docpath"
$opendoc.saveas([ref]"$savepath", [ref]$saveFormat);
$opendoc.close();
}
Recently, while testing, my job stalled because one of the documents is locked by “another user”. I get a dialog box asking me if I want a read ony copy, if I want to edit and merge changes later, or wait for notification that the document is available again. Also, in either case where I choose to open the file, that seems to override my $word.Visible-$False parameter.
I’ve been unsuccessful in identifying this “another user” that’s locked my document. All the documents are on a local, non-shared drive, so I’m not sure what’s got them locked. Based on where the files live, I’m confident that nothing needs to be edited, and I could just force the file open, then Save As...
Can I use -force as a parameter in this function? Where is the best place to place that parameter?
EDIT: Per JPBlanc below, I tried stopping Word and got three errors per record.
Method invocation failed because [Microsoft.Office.Interop.Word.DocumentClass] doesn't contain a method named 'quit'.
At C:\users\x46332\desktop\sad003.ps1:69 char:18
+ $opendoc.quit <<<< ()
+ CategoryInfo : InvalidOperation: (quit:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Exception calling "SaveAs" with "16" argument(s): "COM object that has been separated from its underlying RCW cannot be
used."
At C:\users\x46332\desktop\sad003.ps1:73 char:20
+ $opendoc.saveas <<<< ([ref]"$savepath", [ref]$saveFormat);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Close" with "3" argument(s): "COM object that has been separated from its underlying RCW cannot be u
sed."
At C:\users\x46332\desktop\sad003.ps1:74 char:19
+ $opendoc.close <<<< ();
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
try adding the readonly variant to the method, it is the second variant, see http://msdn.microsoft.com/en-us/library/office/bb216319(v=office.12).aspx.
ie: