自动恢复后修复 Excel 超链接

问题描述 投票:0回答:1

Mac 上的 Excel 崩溃后,它进行了自动恢复,文件重新打开,但所有链接到网络位置的单元格的链接路径都已更改!

原文链接:

file:///Users/dirk/Synology_ELTERN/

更改链接:

文件:///Users/dirk/Library/Containers/com.microsoft.Excel/Data/Library/Application Support/Microsoft/

手动更正此问题非常令人沮丧,特别是当您有很多超链接时

我尝试查找和替换,但这似乎根本不是一个选项,因为根本找不到链接

所以我第一次尝试VBA,但也没有取得多大成功

Sub FixHyperlinks()
    Dim link As Hyperlink
    For Each link In ActiveSheet.Hyperlinks
        link.Address = Replace(link.Address, "file:///Users/dirk/Library/Containers/com.microsoft.Excel/Data/Library/Application Support/Microsoft/", "file:///Users/dirk/Synology_ELTERN/")
    Next link
End sub

我对宏完全陌生,我真的在黑暗中跌跌撞撞。任何帮助/建议将非常感激!

祝大家周末愉快,非常感谢!

excel vba macos hyperlink
1个回答
0
投票

在 ChatGPT 的帮助下,我能够修复超链接:-)

Sub FixHyperlinks()
Dim link As Hyperlink
Dim oldPrefix As String
Dim newPrefix As String
Dim correctedAddress As String
Dim count As Integer

oldPrefix = "../Library/Containers/com.microsoft.Excel/Data/Library/Application Support/Microsoft/"
newPrefix = "//Users/dirk/Synology_ELTERN/"  ' Adjust this to the new path prefix

count = 0

For Each link In ActiveSheet.Hyperlinks
    ' Check if the hyperlink contains the old prefix
    If InStr(1, link.Address, oldPrefix) > 0 Then
        ' Generate the corrected address
        correctedAddress = Replace(link.Address, oldPrefix, newPrefix)
        
        ' Additional check to avoid duplication of base path (optional but safer)
        If Not InStr(1, correctedAddress, newPrefix & newPrefix) > 0 Then
            link.Address = correctedAddress
            count = count + 1
        Else
            ' In case of detected duplication, correct it
            correctedAddress = Replace(correctedAddress, newPrefix & newPrefix, newPrefix)
            link.Address = correctedAddress
            count = count + 1
        End If
    End If
Next link

MsgBox count & " hyperlinks corrected."

结束子

© www.soinside.com 2019 - 2024. All rights reserved.