View Full Version : Change extension in linked images
LibrarianBri
07-12-2017, 07:46 PM
Hello, is there any way to write a script to change the extensions for linked images in a document? I have around 2000 linked images and I found that I should have had them as jpg files not png files. :(
*Edit*
I found this code that apparently does it for Powerpoints... could someone convert it so it works with word?
Sub changeLinkTargets()
Dim pptSlide As Slide
Dim pptShape As Shape
Dim oldString As String
oldString = "\\serverXY\DataFiles"
Dim newString As String
newString = "\\serverAQ\DataFiles"
For Each pptSlide In ActivePresentation.Slides
For Each pptShape In pptSlide.Shapes
If pptShape.Type = msoLinkedOLEObject Or pptShape.Type = msoLinkedPicture Then
With pptShape.LinkFormat
If InStr(1, UCase(.SourceFullName), UCase(oldString)) Then
.SourceFullName = Replace(.SourceFullName, oldString, newString)
End If
End With
End If
DoEvents
Next pptShape
DoEvents
Next pptSlide
End Sub
Found at: https://www.experts-exchange.com/questions/25843323/VBA-to-change-source-of-links-in-PowerPoint.html
gmayor
07-12-2017, 10:25 PM
Changing the extension of an image file does not change the image itself. If the folder that contains the jpg versions used in the document also contains the png versions of the same files then it may be possible to change the extension and use instead the png versions.
Because of the way Word DOCX format handles linked graphics, this is not an easy task, however if you save the document as DOC (Word 97-2003 format) the link fields become accessible.
The glaring problem with this is that this format does not support document components (such as content controls) which were introduced in later file formats; and so if you have such components they will be lost and that might produce a document that is just as bad, albeit in a different way, from that you have now.
If that is not a problem, the following macro should achieve what you require, but RUN IT ON A COPY of the document and not the original!
Sub ChangeExt()
'Graham Mayor - http://www.gmayor.com - Last updated - 13 Jul 2017
Dim oFld As Field
Dim oDoc As Document
Dim strPath As String
Dim strTempPath As String
Set oDoc = ActiveDocument
If oDoc.Path = "" Then oDoc.Save
strPath = oDoc.FullName
strTempPath = Left(strPath, InStrRev(strPath, Chr(46))) & "doc"
oDoc.SaveAs2 FileName:=strTempPath, fileFormat:=0
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldIncludePicture Then
oFld.code.Text = Replace(oFld.code.Text, ".jpg", ".png")
oFld.Update
End If
DoEvents
Next oFld
oDoc.SaveAs2 FileName:=strPath, fileFormat:=12, CompatibilityMode:=val(Application.Version)
lbl_Exit:
Set oDoc = Nothing
Set oFld = Nothing
Exit Sub
End Sub
LibrarianBri
07-12-2017, 10:56 PM
Hello thanks for the quick reply.
I tested it out on a 10 page document that just has 10 images linked in the header. No other content in the document. All the files are called preview***.png X being numbers. I moved all the jpgs to the same directory as the png files. All the files are exactly the same, they are just JPG files instead of the old PNG files (the jpg files are smaller in filesize).
Saved the document as a doc file, checked out the links before (all png) ran the script then looked at the links after, still png files and not the jpg files.
gmayor
07-12-2017, 11:19 PM
Sorry - my fault. I misread the requirement
Change the line
oFld.code.Text = Replace(oFld.code.Text, ".jpg", ".png")
to
oFld.code.Text = Replace(oFld.code.Text, ".png", ".jpg")
LibrarianBri
07-13-2017, 10:52 PM
Changed the line around and still did not change the file extensions. I'm checking the extensions by going to File > Edit Links to Images and they still show png after running the script.
gmayor
07-15-2017, 08:35 PM
How are the images linked? If they are linked by inserting the image as a link, the code I posted should work. Can you supply a document sample?
LibrarianBri
07-20-2017, 07:47 PM
Hello, sorry about the delay in response, was out of town on a vacation. The images are inserted into the header of the document and linked. Here is a sample file:
19828
gmayor
07-20-2017, 08:45 PM
Having seen the document (or rather not seen it as the linked images are not available to the forum) You will have to delete and re-apply the images, and frankly as the images are not included in the document, their file sizes shouldn't be an issue as they are called from file to display them. However, the following macro should do the job. Change the portrait and landscape image file paths as appropriate. The macro assumes that there are only two images used (as seems to be the case in your document). If there are more then you will have to add them and conditionally insert them according to section, much as I have shown with portrait and landscape.
Sub ReplacePicture()
'Graham Mayor - http://www.gmayor.com - Last updated - 21 Jul 2017
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oShape As Shape
Dim t As Long, l As Long, h As Long, w As Long
Dim sName As String, sPicture As String
Const strPathP As String = "C:\Path\PortraitPicture.jpg"
Const strPathL As String = "C:\Path\LandscapePicture.jpg"
For Each oSection In ActiveDocument.Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
For Each oShape In oHeader.Shapes
With oShape
t = .Top
l = .Left
h = .Height
w = .Width
sName = .Name
End With
oShape.Delete
If oSection.PageSetup.Orientation = wdOrientLandscape Then
sPicture = strPathL
Else
sPicture = strPathP
End If
Set oShape = oHeader.Shapes.AddPicture(FileName:=sPicture, _
LinkToFile:=msoTrue, _
SaveWithDocument:=msoFalse, _
Left:=l, _
Top:=t, _
Width:=w, _
Height:=h, _
Anchor:=oHeader.Range)
oShape.Name = sName
Next oShape
End If
Next oHeader
Next oSection
lbl_Exit:
Set oShape = Nothing
Set oHeader = Nothing
Set oSection = Nothing
Exit Sub
End Sub
LibrarianBri
07-21-2017, 11:50 AM
So the document I uploaded was just a sample document. The actual file is over 2000 pages and has lots of pages that are either landscape or horizontal, but all the images have the same name ex: page1.png, page2.png etc. How would I make it loop through the images and replace them instead of just hardcoding in the two file names that need to be changed?
gmayor
07-21-2017, 08:50 PM
The headers are associated with sections rather than pages. Each section can have up to three headers. How do the images relate to the sections and those headers, as there does not appear to be a way to recover the original filenames from the links?
You have to work on the premise that the old names are not known. If you can provide a means to associate the required new images with their section headers then the macro should be easy enough to modify. As it stands it replaces all landscape page headers with one file, all portrait page headers with another.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.