Reputation: 212
I would like to copy a content form a docx to a txt file. I have the following issue:
after $doc = $wd.Documents.Open($path)
is run a dialog box opens saying the document is already used. I have to manually select to open a read-only version, save the file as new file or get informed when the document is ready (standard Windows message)
I dont have the file open.
I do have a solution for this: I run one line of code: Get-Process -Name "*word*" | Stop-Process
My question: I am sure there is a more elegant way to do this correctly, right? In my solution running the code would stop me from using any docx when running the script...
$path="C:\test\Test.docx"
$destination="C:\Test\File1.txt"
$wd = New-Object -COM 'Word.Application'
$doc = $wd.Documents.Open($path)
$content=$doc.Content.Text
$doc.Close()
Set-Content -Path $destination -Value $content
Upvotes: 1
Views: 397
Reputation: 8858
I have personally been bitten by word not fully letting go of my document. Double check it's not your test code that is causing the issue.
In addition to closing the document, also close the word instance.
$wd.quit()
Additionally you can force garbage collection.
[System.GC]::Collect()
Upvotes: 2