You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过PowerShell更新PowerPoint中的超链接求助

PowerShell批量修改PPTX超链接URL失败问题

我尝试用PowerShell修改指定文件夹中所有PPTX文件的超链接URL,将旧的局域网路径替换为SharePoint链接,但遇到了两个问题:

  • 直接修改Hyperlink.Address属性完全没有效果
  • 尝试修改TextToDisplay时抛出错误:

Hyperlink.TextToDisplay : Invalid request. This kind of object cannot have a hyperlink associated with it.

目前的代码只能在旧超链接文本后添加新的URL字符串,但无法将新字符串设置为超链接,添加的只是纯文本。

以下是我使用的PowerShell代码:

Add-type -AssemblyName office

$Powerpoint = New-Object -ComObject powerpoint.application
$msoTrue = [Microsoft.Office.Core.MsoTriState]::msoTrue
$msoFalse = [Microsoft.Office.Core.MsoTriState]::msoFalse
$Powerpoint.visible = $msoTrue

$path = "C:\tmp\ppt"

Get-ChildItem -Path $path -Include "*.pptx" -Recurse |
ForEach-Object {
    "Modifying $_.FullName"
    #Open presentation with ReadOnly:False, Untitled:False, Visible:True
    $presentation = $Powerpoint.Presentations.Open($_.fullname, $msoFalse, $msoFalse, $msoTrue)
    
    $OldStr = "\\PLM\MeetingMinutes\"
    $NewStr = "https://corp.sharepoint.com/:v:/r/teams/MeetingMinutes/"

    foreach($slide in $presentation.slides)
    {
        foreach ($h in $slide.Hyperlinks)
        {
            $ppMouseClick=1
                
            if( $h.Address -like "*$OldStr*" )
            {
                $newUrl =  (($h.Address -replace([Regex]::Escape($OldStr), ($NewStr)))) -replace ("\","/")
                
                if( $h.Parent.Parent.Parent.Parent.HasTextFrame )
                {
                    $_ = $h.Parent.Parent.Parent.Parent.TextFrame.TextRange.Replace($OldStr, "")
                    $text = $h.Parent.Parent.Parent.Parent.TextFrame.TextRange.InsertAfter("`n"+ $newUrl)
                    $text.Font.Size = 10
                    $actionSettings = $text.ActionSettings($ppMouseClick)
                    $actionSettings.Hyperlink.Address = $newUrl
                    $actionSettings.Hyperlink.TextToDisplay  = $newUrl
                    $actionSettings.Action = 7          
                    
                    $urlStart = $text.Find($NewStr)
                    if( $null -ne $urlStart )
                    {
                        $urlStart.Font.Size=7
                    }
                }
            }
        }
    }
    $presentation.Save()
    $presentation.Close()
}
$Powerpoint.Quit()
[gc]::collect()
[gc]::WaitForPendingFinalizers()

内容的提问来源于stack exchange,提问作者Alois Kraus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 10:25:06