Editing path of linked graphics

Springbok 1 Reputation point
2021-11-03T10:28:48.213+00:00

Dear experts,

We have a problem with paths of linked graphics to Word documents (version: 2019).
All graphics are linked to a directory named Graphics, saved in the same directory as the Word files, so that path is relative: "Graphics/graphic_name.jpg". After sending the full direcory somewhere, paths often become absloute: "C:\X\Y\Z\Graphics\graphic_name.jpg".

In the old days, I would select all, hit Shift+F9 and replace all unnecessary data with nothing. Then this option was disabled in the newer versions of Word.

There is a solution of saving as .doc, editing and then saving back to .docx, but it's awfully time consuming when we produce hundreds of files on corporate level.

Is there a VBA solution, which could enable editing path details, i.e. deleting everything between the first (") character and last () character?
Thank you!

{count} votes

1 answer

Sort by: Most helpful
  1. John Korchok 5,661 Reputation points
    2021-11-04T17:01:27.597+00:00

    Give this macro a try:

    Sub ResetLinks()
        Dim x As Long
    
        With ActiveDocument
            For x = .InlineShapes.Count To 1 Step -1
                With .InlineShapes(x)
                    If Not .LinkFormat Is Nothing Then
                        With .LinkFormat
                            If Left(.SourceFullName, 2) <> "\\" Then
                                GraphicsNamePosition = InStrRev(.SourceFullName, "\Graphics", , vbTextCompare)
                                .SourceFullName = "\\" & Right(.SourceFullName, (Len(.SourceFullName) - CInt(GraphicsNamePosition)))
                            End If
                        End With
                    End If
                End With
            Next x
        End With
    End Sub
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.