PDA

View Full Version : Deleting a page if it has no underlined words



colmguckian
07-07-2011, 02:40 AM
I am modifing a word document from excel
I have created bookmarks which represent each page.
I would like a code which deletes all the pages that contains no underlined text.
Here is what I have so Far

Sub FindUnderlined()
Dim wdApp As Word.Application
Set wdApp = GetObject(, "Word.Application")
wdApp.Visible = True

Dim I As Integer
For I = 1 To Documents("MK").Bookmarks.Count
Documents("MK").Bookmarks("Page" & I).Select
Word.Application.Selection.Find.ClearFormatting
With Word.Application.Selection.Find
.Font.Underline = wdUnderlineSingle
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Word.Application.Selection.Find.Execute
If Word.Application.Selection.Find.Found = False Then
Word.Application.Documents("MK").Bookmarks("Page" & I).Select
Selection.Delete
End If
Next I
End Sub

The code works fine until it gets to the highlighted If Statement
but the If statement does not delete the pages with no underlined text.
I would be greatful for any help

macropod
07-08-2011, 04:02 AM
Hi col,

Try something based on:
Sub FindUnderlined()
Dim wdApp As Word.Application
Set wdApp = GetObject(, "Word.Application")
Dim wdDoc As Word.Document
Set wdDoc = wdApp.Documents("MK")
Dim wdRng As Word.Range
Set wdRng = wdDoc.Range(0, 0)
Dim I As Integer
wdApp.ScreenUpdating = False
With wdDoc
For I = .ComputeStatistics(wdStatisticPages) To 1 Step -1
Set wdRng = wdRng.GoTo(What:=wdGoToPage, Name:=I)
Set wdRng = wdRng.GoTo(What:=wdGoToBookmark, Name:="\page")
With wdRng
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Underline = wdUnderlineSingle
.Format = True
.Forward = True
.Wrap = wdFindStop
.Execute
End With
If .Find.Found = False Then .Delete
End With
Next I
End With
wdApp.ScreenUpdating = True
Set wdRng = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing
End Sub

Note: With this code, there is no need to bookmark anything.

PS: When posting code, please use the 'VBA' tags.

colmguckian
07-11-2011, 12:23 AM
Thanks for your help
your code did exactly what I needed