Thread: [Solved] Batch Hyperlinking files
View Single Post
 
Old 04-15-2020, 04:21 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,343
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Option Explicit
Dim FSO As Object, oFolder As Object, StrFolds As String, StrFldr As String, wdDoc As Document
 
Sub Main()
Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True
Dim SubFldrs As Variant, SubFldr As Variant, i As Long
Set wdDoc = ActiveDocument
StrFldr = wdDoc.Path: StrFolds = vbCr & StrFldr
If FSO Is Nothing Then
  Set FSO = CreateObject("Scripting.FileSystemObject")
End If
'Get the sub-folder structure
Set SubFldrs = FSO.GetFolder(StrFldr).SubFolders
For Each SubFldr In SubFldrs
  RecurseWriteFolderName (SubFldr)
Next
'Process the documents in each folder
For i = 1 To UBound(Split(StrFolds, vbCr))
  Call UpdateDocList(CStr(Split(StrFolds, vbCr)(i)))
Next
With wdDoc.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "[^13]{2,}"
  .Replacement.Text = "^p"
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchWildcards = True
  .Execute Replace:=wdReplaceAll
End With
Set wdDoc = Nothing
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
End Sub
 
Sub UpdateDocList(strFolder As String)
Dim strFile As String, strRelPath As String, Fld As Field, Rng As Range, h As Long
strRelPath = Replace(strFolder & "\", StrFldr & "\", "")
h = UBound(Split(strRelPath & "\", "\"))
With wdDoc.Range
  If h = 1 Then
    .InsertAfter vbCr & "This Folder" & vbCr
  Else
    .InsertAfter vbCr & strRelPath & vbCr
  End If
  .Paragraphs.Last.Previous.Style = "Heading " & h
End With
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> wdDoc.FullName Then
    With wdDoc.Range
      .InsertAfter vbCr
      Set Rng = .Hyperlinks.Add(.Characters.Last, "/../" & strRelPath & strFile, , , Split(strFile, ".doc")(0)).Range
      With Rng
        .MoveStartUntil "/", wdForward
        .Collapse wdCollapseStart
        .Fields.Add Range:=.Duplicate, Type:=wdFieldEmpty, Text:="FILENAME \p", PreserveFormatting:=False
      End With
    End With
  End If
  strFile = Dir()
Wend
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
 
Sub RecurseWriteFolderName(aFolder)
Dim SubFolders As Variant, SubFolder As Variant
Set SubFolders = FSO.GetFolder(aFolder).SubFolders
StrFolds = StrFolds & vbCr & CStr(aFolder)
On Error Resume Next
For Each SubFolder In SubFolders
  RecurseWriteFolderName (SubFolder)
Next
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote