Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I delete a section in Word VBA?

Status
Not open for further replies.

clhare

Technical User
May 29, 2003
118
US
I have a document that contains text, then four .tif images, then more text. I need the macro to delete the .tif images in a given situation. I tried to bookmark them from just above the first .tif image to just below the last one, but when I run the macro I get a Dr. Watson error.

Is it possible to delete a specific section in the document via macro? Is there some other way to get the macro to delete these images without an error?

Your help is greatly appreciated!

Cheryl
 
Have you tried the macro recorder ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
To do what? I just tried using the recorder to go to section 4, but how would I select all of the section?

There's another part of my document (which is text) which I can delete by bookmarking it and deleting the bookmarked area. This way won't work for the bookmarked area with the tifs in it.
 
Yes there is.

1. Are the TIF images in their own section - with nothing else you need?

If yes, move the cursor into that section, and run:

activedocument.bookmarks("\section").delete

This delete the current section.

2. If they are not in their own section, put a section break immediately before them, and immediately after them, then use #1.

3. Are there any other graphics in the document that you want to keep?

If there are not then use:

Code:
Sub DeleteAllGraphics()
Dim i As Integer
Dim var
i = ActiveDocument.InlineShapes.Count
    Selection.HomeKey Unit:=wdStory

For var = 1 To i
    Selection.GoTo What:=wdGoToGraphic, Which:=wdGoToNext, Count:=1, Name:=""
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
    End With
    Selection.Delete Unit:=wdCharacter, Count:=1
Next
End Sub

Gerry
 
Yes, the tifs are in their own section. I'm not sure I understand how that code is supposed to appear. If it's section 4, what would the code be? What if there's 2 sections 3 and 4) that I need to delete?
 
Perhaps you should read up on sections.

As stated, with #1, the code deletes the CURRENT section. And, to make it simple, the current section, as stated, is the one with the cursor it it. I don't know how to make it clearer than that that. If you have 2 sections...well then go to each section, and run the code. It deletes the current section.

Just write a simple Sub:

Code:
Sub DeleteThisSection()
Dim r As Range
Set r = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("\section").Range.Start, _
                End:=ActiveDocument.Bookmarks("\section").Range.End)
    r.Delete
set r = Nothing
End Sub

Give a keyboard shortcut, or drag it up to a toolbar as a button. Put your cursor in the section you want to delete, and run the thing.

If you know what sections you want to delete, then run code to specify them, like the following. This deletes sections 2, and 3.

Code:
Sub DeleteSections()
    ActiveDocument.Sections(2).Range.Delete
    ActiveDocument.Sections(3).Range.Delete
    
End Sub


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top