locked
CHANGE THE PATH OF ALL HYPERLINK RRS feed

  • Question

  • I changed  the file server with another that has the same name "nas01" and the same ip address .

    In the old server there was a shared folder : \\nas01\gruppi$

    In the new server there is a shared folder:  \\nas01\gruppi     (without $)  

    I copied all folders from Old Server to New Server.

    In the MS Word's document there are several  pictures with hyperlink and when I open them, they search the path of all pictures in \\nas01\gruppi$  

    How do to change all links from:    \\nas01\gruppi$        to:    \\nas01\gruppi

    Thank you for help 

     
    Friday, March 4, 2016 2:56 PM

Answers

  • You will need to batch-process all the files, opening them and changing the link paths. You should also be aware that the server path change may adversely impact the opening of any documents that look for templates using the old path. When templates other than Word’s Normal template are used to create a document, the template’s path & name are stored with the document. If that path is a network path, a change to the server name will break the link. The result can be significant delays in opening the documents on the new server. See: http://support.microsoft.com/?kbid=830561. The same effect occurs when the file is opened on a computer attached to a different network.

    The following macro can be used to update the template paths or, if a new template path can’t be found, to point it to Word’s Normal template. Code is included to restore the original date/time stamps of the updated files. It also updates the paths for linked objects, as per your specification.

    In the code, simply replace however much of the old & new template paths differ in the OldServer and NewServer variables. You can determine how much of the old & new template paths differ, for the purposes of the OldServer and NewServer variables with code like the following, which you can run on a document created on the old server and another created on the new server, both referencing the same template (for all I know you may need do nothing more than change 'nas01\gruppi$' to 'nas01\gruppi').

    Sub GetTemplateRef()
    With ActiveDocument
      MsgBox Dialogs(wdDialogToolsTemplates).Template
    End With
    End Sub

    ******************

    Dim FSO As Object 'a FileSystemObject
    Dim oFolder As Object 'the folder object
    Dim oSubFolder As Object 'the subfolders collection
    Dim oFiles As Object 'the files object
    Dim i As Long, j As Long

    Sub Main()
    ' Minimise screen flickering
    Application.ScreenUpdating = False
    Dim StrFolder As String
    ' Browse for the starting folder
    StrFolder = GetTopFolder
    If StrFolder = "" Then Exit Sub
    i = 0: j = 0
    ' Search the top-level folder
    Call GetFolder(StrFolder & "\")
    ' Search the subfolders for more files
    Call SearchSubFolders(StrFolder)
    ' Return control of status bar to Word
    Application.StatusBar = ""
    ' Restore screen updating
    Application.ScreenUpdating = True
    MsgBox i & " of " & j & " files updated.", vbOKOnly
    End Sub

    Function GetTopFolder() As String
    GetTopFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetTopFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function

    Sub SearchSubFolders(strStartPath As String)
    If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = FSO.GetFolder(strStartPath)
    Set oSubFolder = oFolder.subfolders
    For Each oFolder In oSubFolder
      Set oFiles = oFolder.Files
      ' Search the current folder
      Call GetFolder(oFolder.Path & "\")
      ' Call ourself to see if there are subfolders below
      Call SearchSubFolders(oFolder.Path)
    Next
    Set FSO = Nothing
    End Sub

    Sub GetFolder(StrFolder As String)
    Dim strFile As String
    strFile = Dir(StrFolder & "*.doc")
    ' Process the files in the folder
    While strFile <> ""
      ' Update the status bar is just to let us know where we are
      Application.StatusBar = StrFolder & strFile
      Call UpdateRefs(StrFolder & strFile)
      strFile = Dir()
    Wend
    End Sub

    Sub UpdateRefs(strDoc As String)
    ' This sub updates the template paths for files after a server
    ' change. Simply insert however much of the lower end of the
    ' server paths differ as the OldServer and NewServer variables.
    Dim OldServer As String, NewServer As String, strPath As String
    Dim oItem As Object, StrDtTm As String
    Dim Rng As Range, Shp As Shape, iShp As InlineShape
    OldServer = "old server path": NewServer = "new server path"
    ' Store the file's current Date/Time stamp.
    If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
    Set oItem = FSO.GetFile(strDoc)
    StrDtTm = oItem.DateLastModified
    ' Open the document
    Documents.Open strDoc, AddToRecentFiles:=False, ReadOnly:=False, Format:=wdOpenFormatAuto
    With ActiveDocument
      If .ProtectionType = wdNoProtection Then
        ' Update the template path
        strPath = Dialogs(wdDialogToolsTemplates).Template
        If LCase(Left(strPath, Len(OldServer))) = LCase(OldServer) Then
          ' Update the file counter for changed files
          i = i + 1
          ' Get the new template path & name
          strPath = NewServer & Mid(strPath, Len(OldServer) + 1)
          ' Check whether the template exists
          If Dir(strPath) <> "" Then
            ' If found, update the path
            .AttachedTemplate = NewServer & Mid(strPath, Len(OldServer) + 1)
          Else
            ' If not found, reset the template to 'Normal'
            .AttachedTemplate = ""
            ' Output an error report in the document from which the macro is run.
            ThisDocument.Range.InsertAfter vbCr & "Template: " & strPath & " not found for " & strDoc
          End If
        End If
        'update the link paths for all linked objects
        For Each Rng In .StoryRanges
          With Rng
            For Each Shp In .ShapeRange
              With Shp
                If Not .LinkFormat Is Nothing Then
                  With .LinkFormat
                    .SourcePath = Replace(.SourcePath, "nas01\gruppi$", "nas01\gruppi")
                  End With
                End If
              End With
            Next
            For Each iShp In .InlineShapes
              With iShp
                If Not .LinkFormat Is Nothing Then
                  With .LinkFormat
                    .SourcePath = Replace(.SourcePath, "nas01\gruppi$", "nas01\gruppi")
                  End With
                End If
              End With
            Next
          End With
        Next
      Else
        ' Output a 'protected' file report in the document from which the macro is run.
        ThisDocument.Range.InsertAfter vbCr & strDoc & " protected. Not updated."
      End If
      .Close SaveChanges:=True
    End With
    ' Update the main file counter
    j = j + 1
    ' Let Word do its housekeeping
    DoEvents
    ' Reset the file's Date/Time stamp.
    Set oItem = FSO.GetFile(strDoc)
    If oItem.DateLastModified <> StrDtTm Then oItem.DateLastModified = StrDtTm
    Set oItem = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]



    • Edited by macropodMVP Saturday, March 5, 2016 3:23 AM
    • Proposed as answer by Steve Fan Tuesday, March 15, 2016 9:05 AM
    • Marked as answer by Steve Fan Wednesday, March 16, 2016 9:46 AM
    Saturday, March 5, 2016 3:21 AM