Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-10-2020, 01:05 PM
alex100 alex100 is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2016
Advanced Beginner
Remove all the images in a document, including the new lines
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default 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
The problem is that I also need to remove the line where the image used to be, so that the paragraphs before and after the image will not be divided by a blank new line. Simply deleting the "[IMAGE REMOVED]" replacement text won't do that, as it will leave a new line in that spot.

What's the best way to do this, please?



Thank you,
Alex
Reply With Quote
  #2  
Old 05-10-2020, 03:50 PM
macropod's Avatar
macropod macropod is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 05-11-2020, 12:42 AM
alex100 alex100 is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2016
Advanced Beginner
Remove all the images in a document, including the new lines
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 05-11-2020, 12:51 AM
macropod's Avatar
macropod macropod is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

In which case, you can add the applicable Find/Replace code to your existing script.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-11-2020, 01:40 AM
alex100 alex100 is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2016
Advanced Beginner
Remove all the images in a document, including the new lines
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

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
Thank you!

Alex
Reply With Quote
  #6  
Old 05-11-2020, 01:53 AM
macropod's Avatar
macropod macropod is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 05-11-2020, 04:46 AM
alex100 alex100 is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2016
Advanced Beginner
Remove all the images in a document, including the new lines
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Indeed, this one works good, it removed all the images while testing it!

Alex
Reply With Quote
  #8  
Old 05-11-2020, 09:23 AM
gmaxey gmaxey is online now Remove all the images in a document, including the new lines Windows 10 Remove all the images in a document, including the new lines Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 05-11-2020, 09:55 AM
alex100 alex100 is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2016
Advanced Beginner
Remove all the images in a document, including the new lines
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Thank you too, Greg! Much appreciated!

Alex
Reply With Quote
  #10  
Old 05-11-2020, 03:18 PM
macropod's Avatar
macropod macropod is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #11  
Old 05-25-2020, 12:00 PM
alex100 alex100 is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2016
Advanced Beginner
Remove all the images in a document, including the new lines
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

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
Reply With Quote
  #12  
Old 05-25-2020, 02:59 PM
macropod's Avatar
macropod macropod is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #13  
Old 05-25-2020, 03:08 PM
gmaxey gmaxey is online now Remove all the images in a document, including the new lines Windows 10 Remove all the images in a document, including the new lines Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #14  
Old 05-26-2020, 01:22 AM
alex100 alex100 is offline Remove all the images in a document, including the new lines Windows 7 64bit Remove all the images in a document, including the new lines Office 2016
Advanced Beginner
Remove all the images in a document, including the new lines
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

I hope it's not just me benefiting from these two image removal routines, but others will benefit as well.

Thank you both!

Alex
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove all the images in a document, including the new lines How do I Remove A Specific Amount of Characters from Lines in a Document? tatihulot Word 5 01-22-2016 05:55 AM
Remove all the images in a document, including the new lines 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:16 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft