View Single Post
 
Old 03-02-2023, 09:22 PM
yondor yondor is offline Windows 10 Office 2021
Novice
 
Join Date: Mar 2023
Posts: 2
yondor is on a distinguished road
Default

After looking some more on this forum, I tried the following code:
(source:https://www.msofficeforums.com/word-...ocx-files.html)

Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName: strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      'Call your other macro or insert its code here
      Call Update_Links
      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
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

Using the above to call the below code. The below alone already works for me on a single word doc:

Code:
Sub Update_Links()
'
' Update_Link Macro
'
Dim Rng As Range, i As Long, vItem As Variant
'
'Display File Picker Dialog
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  ' use Show method to dispaly File Picker dialog box and return user's action
  If .Show = -1 Then
  'step throug each string in the FileDialogSelectedItems collection
    vItem = .SelectedItems(1)
  Else
    Exit Sub
  End If
End With
'
'update fields
'
Application.ScreenUpdating = False
With ActiveDocument
  For Each Rng In .StoryRanges
    With Rng
      For i = .Fields.Count To 1 Step -1
        If .Fields(i).Type = wdFieldLink Then
          .Fields(i).LinkFormat.SourceFullName = vItem
          .Fields(i).Update
        End If
      Next
      For i = .ShapeRange.Count To 1 Step -1
        If Not .ShapeRange(i).LinkFormat Is Nothing Then
          .ShapeRange(i).LinkFormat.SourceFullName = vItem
          .ShapeRange(i).LinkFormat.Update
        End If
      Next
      For i = .InlineShapes.Count To 1 Step -1
        If Not .InlineShapes(i).LinkFormat Is Nothing Then
          .InlineShapes(i).LinkFormat.SourceFullName = vItem
          .InlineShapes(i).LinkFormat.Update
        End If
      Next
    End With
  Next
End With
'
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub

However, the UpdateDocuments macro isn't working for me to update all documents. Any thoughts? Apologies, I'm new to this..
Reply With Quote