来自Word Doc的Powershell Select-All

时间:2013-08-02 20:58:25

标签: powershell

我想打开,然后选择word文档中的所有文本,而不是任何属性,格式等。我搜索过这个网站并用Google搜索结果。基本上类似于打开Word文档并按Ctrl-A并将结果分配给变量。

$word = New-Object -ComObject Word.Application
$word.visible = $True
$wordfilepath = "\\symphony1\powershell\Phones\Phone.docx"
$doc = $word.Documents.Open($wordfilepath)
????
$selection" >> $textfilepath

基本上是一个新手问题,但任何人都可以帮忙吗? 感谢。

1 个答案:

答案 0 :(得分:3)

这可能会满足您的需求。它会创建一个新的单词对象,打开现有文件,然后从中提取文本。

$filePath = <your file here>
$doc = New-Object -com word.application
$fileToOpen = $doc.Documents.Open("$filePath")
$text = $fileToOpen.Range().text

预先警告它将删除甚至非常基本的格式化功能,例如新行。这是一个很好的其他range members and properties列表,您可能会发现它很有帮助。