Macro: Remove all the images in a Word document
Thread poster: Hans Lenting
Hans Lenting
Hans Lenting
Netherlands
Member (2006)
German to Dutch
Jan 6, 2023

Sometimes you want to remove all images (e.g. to reduce the file size).

I found this macro:

https://www.msofficeforums.com/word-vba/44918-remove-all-images-document-including-new-lines.html

Works on Mac too.


Public Sub ReplaceAllGraphicsWithNothing()
Dim rngStory As Word
... See more
Sometimes you want to remove all images (e.g. to reduce the file size).

I found this macro:

https://www.msofficeforums.com/word-vba/44918-remove-all-images-document-including-new-lines.html

Works on Mac too.


Public Sub ReplaceAllGraphicsWithNothing()
Dim rngStory As Word.Range
Dim lngJunk As Long
Dim lngIndex As Long
'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
SrcAndRplInStory rngStory, "^g^p", ""
SrcAndRplInStory rngStory, "^g^l", ""
SrcAndRplInStory rngStory, "^g", ""
For lngIndex = rngStory.ShapeRange.Count To 1 Step -1
rngStory.ShapeRange.Item(lngIndex).Delete
Next lngIndex
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
lbl_Exit:
Exit Sub
End Sub
Public Sub SrcAndRplInStory(ByVal rngStory As Word.Range, _
ByVal strSearch As String, _
ByVal strReplace As String)
With rngStory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearch
.Replacement.Text = strReplace
.Execute Replace:=wdReplaceAll
End With
lbl_Exit:
Exit Sub
End Sub


[Edited at 2023-01-06 08:39 GMT]
Collapse


Dalia Nour
 
Dalia Nour
Dalia Nour  Identity Verified
Egypt
Local time: 17:41
Member (2018)
English to Arabic
+ ...
Thank you! Jan 6, 2023

Thanks for sharing, Hans!

 
Stepan Konev
Stepan Konev  Identity Verified
Russian Federation
Local time: 17:41
English to Russian
In addition Jan 6, 2023

You can also replace ^g with blank field to remove images.

Gerard de Noord
 
Hans Lenting
Hans Lenting
Netherlands
Member (2006)
German to Dutch
TOPIC STARTER
Didn't work Jan 7, 2023

Stepan Konev wrote:

You can also replace ^g with blank field to remove images.


The documents were old .doc files. If I'm not mistaken, Microsoft has changed the way graphics are embedded in layers since then.

Anyway, this didn't work (in Page View):

Screen Shot 2023-01-07 at 06.42.45

The macro does work. And I like to combine tiny tasks, which is only possible with macros:


Sub Test()
'
' This macro is assigned to Ctrl+Alt+T
' Do not delete this macro!
'

SaveActiveDocumentAsDocx
ReplaceAllGraphicsWithNothing
ActiveDocument.save

ActiveDocument.Close

End Sub


 
Stepan Konev
Stepan Konev  Identity Verified
Russian Federation
Local time: 17:41
English to Russian
Macro uses ^g too Jan 7, 2023

Hans Lenting wrote:
The macro does work.
Weirdly it works for me... Probably it somehow relates to the format of images. However, as far as I can see now from the code, the macro uses exactly the same ^g. Right?

Update:
Ah, ok, I have read the entire discussion. I see now. The macro also removes all floating images. When I prepare a file for translation, I make all images inline. That's is why simple replacement of ^g with a blank value works for me. However, if you have both inline and floating images, then the macro is a better choice indeed.

[Edited at 2023-01-07 06:19 GMT]


Hans Lenting
 
Hans Lenting
Hans Lenting
Netherlands
Member (2006)
German to Dutch
TOPIC STARTER
Yep Jan 7, 2023

Stepan Konev wrote:

Hans Lenting wrote:
The macro does work.
Weirdly it works for me... Probably it somehow related to the format of images. However, as far as I can see now from the code, the macro uses exactly the same ^g. Right?


Yes. But obviously, strange things DO happen.


Stepan Konev
 


To report site rules violations or get help, contact a site moderator:


You can also contact site staff by submitting a support request »

Macro: Remove all the images in a Word document






TM-Town
Manage your TMs and Terms ... and boost your translation business

Are you ready for something fresh in the industry? TM-Town is a unique new site for you -- the freelance translator -- to store, manage and share translation memories (TMs) and glossaries...and potentially meet new clients on the basis of your prior work.

More info »
Trados Business Manager Lite
Create customer quotes and invoices from within Trados Studio

Trados Business Manager Lite helps to simplify and speed up some of the daily tasks, such as invoicing and reporting, associated with running your freelance translation business.

More info »