Finding and Removing Stubborn Graphics

Written by Allen Wyatt (last updated December 27, 2021)
This tip applies to Word 2007, 2010, 2013, and 2016


5

Anita is re-formatting a long document (over 300 pages) that was converted from PDF to Word. She has lots of experience with Word and styles and using Find and Replace to fix badly formatted documents. In this case there is a problem she can't figure out. The original document had horizontal lines in the header and footer that were converted to graphic lines in the Word document. She wants to delete all those graphic lines, but searching for ^g doesn't find them. Anita wonders if there is a way she can automate the removal of these graphic lines, as she'd rather not have to click and press Del 600+ times.

Since it appears that the PDF conversion process is adding the graphics to the header and footer, that means they are not easily "findable" by doing a regular Find and Replace. Instead, you'll want to use a macro to get rid of them. The following example looks only in the header and footer area and deletes any graphics that it finds there.

Sub FooterHeaderGraphicFind()
    Dim rStory As Range
    Dim i As Integer

    For Each rStory In ActiveDocument.StoryRanges
        If rStory.StoryType = wdPrimaryFooterStory Or _
          rStory.StoryType = wdPrimaryHeaderStory Then
            For i = rStory.Shapes.Count To 1 Step -1
                rStory.Shapes(i).Delete
            Next i
        End If
    Next rStory
End Sub

Note that it deletes all the shapes in the header or footer, not just lines. (There is no way to differentiate the content of one graphic shape from another.)

Of course, there could be a much simpler way to handle the situation, without the need for a macro:

  1. Open the Word document that was converted from the PDF file.
  2. Open a brand new document. (You should now have two documents open at the same time.)
  3. In the converted document, press Ctrl+A. This selects everything in the document.
  4. Press Ctrl+C. This copies everything to the Clipboard.
  5. Switch to the brand new document.
  6. Make sure the Home tab of the ribbon is displayed.
  7. Click the down-arrow under the Paste tool. Word displays a variety of pasting options.
  8. Click the Keep Text Only tool. (It looks like a clipboard with the letter A at the bottom-right.) Word pastes unformatted text from the Clipboard into the target document.

This approach should get rid of any type of graphic and formatting artifacts introduced into the document by the PDF conversion process. The result is a "clean" document that you can format any way you want. This approach is especially easy if you have implemented and can apply styles throughout the document.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13510) applies to Microsoft Word 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Saving Documents Faster

If you are using Word versions 97 through 2003, there's a setting you can make that will allow you to save your documents ...

Discover More

Clearing the Print Area

Excel allows you to specify which portions of a worksheet should be printed when you send output to your printer. If you ...

Discover More

Using a Formula to Replace Spaces with Dashes

If you need a formula to change spaces to some other character, the SUBSTITUTE function fits the bill. Here's how to use it.

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Searching and Replacing Graphics

Got a bunch of graphics in a document that need replacing? (Perhaps you need to replace an old logo with a new one.) Word ...

Discover More

Default Picture Location

When you insert pictures into a document, the first folder that Word opens up is normally the My Pictures folder. You can ...

Discover More

Understanding Page Border Art

Add some artwork around the border of your printed page, and you may not know where that artwork comes from. You may also ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five minus 5?

2022-01-18 12:34:52

Mona

This didn't work for me. Gave me an error on this:

Sub FooterHeaderGraphicFind()


2017-10-17 11:31:46

Alana

Woops, skip the OCR step; just realized it's a PDF converted from Word. Carry on.


2017-10-17 11:29:47

Alana

I like to temporarily crop (or save the file as same filename and adding "_cropped" at the end) the header and footer info on all pages of the PDF file, then convert text to word "OCR recognition" so that I can CTRL+A to select all for copy-paste in Word. Be sure the PDF is in Fit Page Continuous before doing CTRL+A. Hope this helps.


2017-07-01 23:20:17

Isabel Leonard

Easier still: copy the whole document to Notepad then copy it back to Word. Downside: Notepad gets rid of attributes such as bolding and italicization.


2017-07-01 04:58:21

Ken Endacott

The macro given will not work for a couple of reasons.
rStory.Shapes is invalid, it should be rStory.ShapeRange
The macro will only remove shapes from the first Section.

The following macro will remove shapes from all headers and footers in all Sections. The coding does not look intuitive because it appears to work only on the header of the first section, but it actually covers every header, footer and section.

Sub RemoveShapesInHeaderFooter()
Dim aSection As Section
Set aSection = ActiveDocument.Sections(1)
Do While aSection.Headers(wdHeaderFooterPrimary).Shapes.Count > 0
aSection.Headers(wdHeaderFooterPrimary).Shapes(1).Delete
Loop
End Sub


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.