【发布时间】:2021-04-26 13:11:05
【问题描述】:
我正在尝试以递归方式遍历目录结构以查找 word 文档,然后提取超链接。代码执行时输出如下:
processing 2 docs
File Name Hyperlink
--------- ---------
C:\temp\doc1.docx
C:\temp\doc1.docx
C:\temp\folder\doc2.docx
C:\temp\folder\doc2.docx
我尝试过的任何方法似乎都不起作用。我试过使用:
- “超链接”= $_Address
- “超链接”= $thisDoc.Address
- “超链接” = $thisDoc.Hyperlink.Address
Clear-Host
$parentFolder = "C:\temp"
$ourDocs = Get-ChildItem -Recurse -LiteralPath $parentFolder -file -include *.doc*
"processing {0} docs" -f $ourDocs.Count
$word = New-Object -ComObject word.application
$word.Visible = $false
$word.ScreenUpdating = $false
$array = New-Object System.Collections.ArrayList
$ourDocs | ForEach-Object{
$thisDoc = $word.Documents.Open($_.FullName)
$thisDoc.Hyperlinks | ForEach-Object {
$array.Add([pscustomobject]@{
"File Name" = $thisDoc.FullName
"Hyperlink" = $_Address}) | Out-null
}
$thisDoc.Close()
}
$Word.Quit()
$array
# cleanup com objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
【问题讨论】:
标签: powershell ms-word hyperlink