Ok, given your example the process would be to enumerate all the hyperlinks in the spreadsheet. For each spreadsheet update the link to have a new prefix (\\server\share
). Something like this.
Sub FixLinks()
Const LinkPrefix = "\\server\share\"
Dim link As Hyperlink
Dim newLink As String
For Each link In ActiveSheet.Hyperlinks
newLink = LinkPrefix & link.Address
link.Address = newLink
Next
End Sub
There are caveats here. Firstly the assumption is that each link in the spreadsheet is just the relative URL and not a mix of absolute and relative URLs. For example if one link is \\server1\share1\folder\file1
and another is folder\file1
then the script would not be able to easily tell the difference. If you need that logic then your script will have to get more complex to try to figure out what the existing link actually is.
Secondly, macros are disabled in Office docs for security reasons. Therefore you'll need to temporarily switch the app type. Open the spreadsheet then go to the Developers tab and click Macros. Then create a new macro (e.g. FixLinks
). Put the contents of the script into the macro. Then save it. When you do Office will tell you to click Yes to ignore the macro or No to enable it. When you click No then it'll prompt for you to save the spreadsheet as a new file. Change the type to Excel Macro-Enabled Workbook
. Once the save is complete then go back to the Macros list, select the macro and click Run to run it. Once it completes your spreadsheet should be updated. At that point you shouldn't need the macro anymore so you can save again and change the type back to just Excel Workbook
to remove the macro.