#1
|
|||
|
|||
Remove all the images in a document, including the new lines
I'm looking for a way to remove all the images in a document. I found this script...
Code:
Dim oILShp As InlineShape For Each oILShp In ActiveDocument.InlineShapes ActiveDocument.Range(oILShp.Range.Start, oILShp.Range.End).Text = "[IMAGE REMOVED]" Next What's the best way to do this, please? Thank you, Alex |
#2
|
||||
|
||||
Why are you inserting '[IMAGE REMOVED]' (which obviously takes up some space) and not using a simple Find/Replace? For example:
Find = ^g^p Replace = nothing
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
I could use the manual 'Find/Replace' function, but I'd like to do this in an automated way. I paste various articles from the web, but I'm only interested in pasting the text information. After I use the paste function, a larger script is run to apply certain formatting options. At this point I'd also like to integrate a small subroutine that will delete all the images found, including the new lines left behind them.
|
#4
|
||||
|
||||
In which case, you can add the applicable Find/Replace code to your existing script.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Yes, I just did that, only with a slight modification to your idea.
The 'Find = ^g^p' pattern did not seem to find and replace all the images in the document. Although I am not 100% sure, I believe it failed to replace those images that were not followed by a new line/paragraph. Anyhow, I'm positive it was something related to how the image and the text that followed it were formatted in the source HTML page. The code below seems to work better, as I tested it quite well. Code:
Dim oILShp As InlineShape For Each oILShp In ActiveDocument.InlineShapes ActiveDocument.Range(oILShp.Range.Start, oILShp.Range.End).Text = "[IMAGE REMOVED]" Next Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "[IMAGE REMOVED]^p" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll Alex |
#6
|
||||
|
||||
Really, that is awful code - all you've done is cobble something together using the macro recorder and tacked it onto the end of your already defective code. If, as you say, some images were not removed, your solution is hardly any better, as those very same images will instead still display '[IMAGE REMOVED]'. All you need is:
Code:
With ActiveDocument.Range.Find .Execute FindText:="^g^p", ReplaceWith:="", Wrap:=wdFindContinue, Replace:=wdReplaceAll .Execute FindText:="^g^l", ReplaceWith:="", Wrap:=wdFindContinue, Replace:=wdReplaceAll .Execute FindText:="^g", ReplaceWith:="", Wrap:=wdFindContinue, Replace:=wdReplaceAll End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Indeed, this one works good, it removed all the images while testing it!
Alex |
#8
|
|||
|
|||
Paul (Macropod) already knows this and can likely improve on this code. While his posted solution solved the OP's issue, for completeness the code provided will not remove all images/graphics/shapes/lines or whatever we want to call them from a document.
While the find string posted will find all inline graphics in the document if run from the user interface, it will only find the inline graphics in the currently selected storyrange if run via VBA. Also the find string posted regardless of how run will not find any floating graphics/shapes. Code:
Public Sub RelaceAllGraphicsWithNothing() 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 |
#9
|
|||
|
|||
Thank you too, Greg! Much appreciated!
Alex |
#10
|
||||
|
||||
I know the code I posted only deals with inline shapes in the document body, but that's all the OP's issue really concerned. And no, it would not process inline shapes in any other selected StoryRange.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
To: Greg Maxey (gmaxey)
The code you posted above works great, but for non-inline images, it will still leave an empty paragraph where the image used to be. How can I change the 'rngStory.ShapeRange.Item(lngIndex).Delete' instruction so that it will not only delete the image itself, but also the paragraph? I'm looking for a solution where there are no additional spaces left in between the document lines, after all images have been removed. Thank you very much! Alex |
#12
|
||||
|
||||
If you want to process floating shapes - including any empty paragraphs they're attached to - as well, try:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument Do While .Shapes.Count > 0 .Shapes(1).ConvertToInlineShape Loop With .Range.Find .Execute FindText:="^g^p", ReplaceWith:="", Wrap:=wdFindContinue, Replace:=wdReplaceAll .Execute FindText:="^g^l", ReplaceWith:="", Wrap:=wdFindContinue, Replace:=wdReplaceAll .Execute FindText:="^g", ReplaceWith:="", Wrap:=wdFindContinue, Replace:=wdReplaceAll End With End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
You would want to ensure the anchor range paragraph doesn't contain text:
Code:
Public Sub RelaceAllGraphicsWithNothing() Dim rngStory As Word.Range Dim lngJunk As Long Dim lngIndex As Long Dim oShp As Shape Dim oPar As Paragraph '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 Set oShp = rngStory.ShapeRange.Item(lngIndex) If Len(oShp.Anchor.Paragraphs(1).Range) = 1 Then Set oPar = oShp.Anchor.Paragraphs(1) oShp.Anchor.Delete oPar.Range.Delete Else oShp.Delete End If Next lngIndex 'Get next linked story (if any) Set rngStory = rngStory.NextStoryRange Loop Until rngStory Is Nothing Next lbl_Exit: Exit Sub End Sub |
#14
|
|||
|
|||
I hope it's not just me benefiting from these two image removal routines, but others will benefit as well.
Thank you both! Alex |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How do I Remove A Specific Amount of Characters from Lines in a Document? | tatihulot | Word | 5 | 01-22-2016 05:55 AM |
How to Prevent Table of Contents Including Document Details | shaaa | Word | 5 | 06-10-2015 03:52 PM |
How to stop including images as attachment (in other e-mail clients)? | TylerSD | Outlook | 0 | 12-05-2014 01:56 PM |
hard lines across document - can't remove | joanieS | Word | 3 | 07-30-2013 11:29 AM |
Remove all images from a Mac OS X Word 2008 Document? | qcom | Drawing and Graphics | 0 | 04-23-2011 06:48 PM |