Macro: Remove all the images in a Word document
Автор темы: Hans Lenting
Hans Lenting
Hans Lenting
Нидерланды
Член ProZ.com c 2006
немецкий => голландский
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
Египет
Local time: 10:33
Член ProZ.com c 2018
английский => арабский
+ ...
Thank you! Jan 6, 2023

Thanks for sharing, Hans!

 
Stepan Konev
Stepan Konev  Identity Verified
Россия
Local time: 10:33
английский => русский
In addition Jan 6, 2023

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

Gerard de Noord
 
Hans Lenting
Hans Lenting
Нидерланды
Член ProZ.com c 2006
немецкий => голландский
Автор темы
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
Россия
Local time: 10:33
английский => русский
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
Нидерланды
Член ProZ.com c 2006
немецкий => голландский
Автор темы
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:

Модератор(ы) этого форума
Maya Gorgoshidze[Call to this topic]
Prachya Mruetusatorn[Call to this topic]

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

Macro: Remove all the images in a Word document






Trados Studio 2022 Freelance
The leading translation software used by over 270,000 translators.

Designed with your feedback in mind, Trados Studio 2022 delivers an unrivalled, powerful desktop and cloud solution, empowering you to work in the most efficient and cost-effective way.

More info »
Protemos translation business management system
Create your account in minutes, and start working! 3-month trial for agencies, and free for freelancers!

The system lets you keep client/vendor database, with contacts and rates, manage projects and assign jobs to vendors, issue invoices, track payments, store and manage project files, generate business reports on turnover profit per client/manager etc.

More info »