如何使用PowerShell编辑Word文档中的超链接? [英] How can I use PowerShell to edit hyperlinks in a Word document?

查看:233
本文介绍了如何使用PowerShell编辑Word文档中的超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一系列3500个html文档转换为Word以获取文档存储库。我们遇到了一个问题,其中一些超链接在转换的后端被破坏,没有明显的原因。我想生成一个文件名列表和每个文件中包含的链接,看看我是否可以发现任何模式并相应地调整我的转换程序。不幸的是,包含PowerShell和超链接的搜索会导致很多关于如何使用Powershell添加超链接的项目,并且没有一种情况适用于我的需求。

I am in the process of converting a series of 3500 html documents to Word for a documentation repository. We've run into a problem where some hyperlinks are broken on the back end of the conversion for no apparent reason. I want to generate a list of filenames and the links contained in each to see if I can spot any patterns and adjust my conversion program accordingly. Unfortunately, searches that include PowerShell and hyperlinks lead to a lot of items about how to ADD hyperlinks using Powershell, and none of the situations have been applicable to my needs.

使用 this链接此链接作为此代码的起点......

Using this link and this link as my starting point with this code....

$word = New-Object -ComObject Word.Application
$document = $word.documents.open("C:\users\administrator\desktop\TEST.docx") 
$document.Hyperlinks 
([uri]"http://domain.com/This is a bad link").AbsoluteUri 
$hyperlinks = @($document.Hyperlinks) 
$hyperlinks | ForEach {
    If ($_.Address -match "\s") {
        $newURI = ([uri]$_.address).AbsoluteUri
        Write-Verbose ("Updating {0} to {1}" -f $_.Address,$newURI) -Verbose
        $_.address = $newURI
    }
}
$document.save()
$word.quit() 

我一直在尝试制作符合要求的东西我的需要。我可以复制上面脚本的结果,但是无法使用 ForEach 命令迭代遍历目录中的所有文档。我正在尝试将所有链接从html更改为doc,但第二个我插入此代码:

I've been trying to craft something that will meet my needs. I can duplicate the above script's results, but have not been able to get a successful run iterating through all the documents in a directory with a ForEach command. I'm trying to change all links from html to doc, but the second I insert this code:

If ($.Address. -match ".\.doc") {
    $newExt = ".doc" ;
    $newURI = ([uri]$$_.address).BaseName.$newExt.

我在运行时出错并命令失败错误。 此链接有帮助,这个链接回答了我对VBA / VBScript的问题...但不是PowerShell。有没有人有这个Powershell解决方案?

I get out of bounds and command failure errors at runtime. This Link helped, and this link answers my question for VBA/VBScript...but not PowerShell. Does anyone have a Powershell solution for this?

推荐答案

可能与以下内容有关:

If ($.Address. -match ".\.doc") {
             ^
    $newExt = ".doc" ;
    $newURI = ([uri]$$_.address).BaseName.$newExt.
                     ^                           ^

为什么不把它重写成这样的东西(你需要找到正确的类型,如超链接你自己)

Why not rewrite it into something like this (you'll need to find the right types like Hyperlink yourself)

$toChange = $document.Hyperlinks | ? { $_.address.endswith('.doc') } | % { $_.address = $_.address.replace('.doc', '.html') }

这篇关于如何使用PowerShell编辑Word文档中的超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆