无法通过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
相关产品推荐
相关产品推荐

