Though this is a very old thread, it's an issue that often arises with Word automation, with difficult pitfalls to be avoided, and I'm not seeing a general solution in this thread. So here's one that can be adapted for problems like Santa Claus's. Note that my demo code doesn't actually use RemoveWholeLines's optional DocRange parameter, but it can be used to limit the document range to which the deletions are applied.
Sub DemoExtractExecutableCodeLines()
'As an example, if VBA code is copied from the VBE editor to the document under which this routine is then
'executed, it will remove all commented lines (except the first one, as noted below) and blank lines, leaving
'only the executable code.
Call RemoveWholeLines("^p'*^p") 'Remove all left-justified, entire-comment lines (except the first one,
'which the user may be using to identify the data set being processed).
Call RemoveWholeLines("^p @'*^p") 'Remove all blank-offset, entire-comment lines (except the first one, as
'noted above).
Call RemoveWholeLines("^p^p") 'Remove all intervening blank lines.
Call RemoveWholeLines("^p @^p") ' "
End Sub
Private Sub RemoveWholeLines(ByVal LinePattern As String, Optional DocRange As Variant)
'Removes entire lines (^p to ^p), in the optionally specified document-range, that match the specified string
'pattern, including any wildcard specifications, and deletes each matching range except for its single,
'trailing ^p (in order to avoid concatenation of adjacent lines). If no document-range is specified, then the
'search range is set to be the entire active document of the file under which this code is executing.
'
'If delimiting "^p" or "^13" characters are not included in the specification, "^13" characters are added.
'The delimiters can be specified as "^p" but will be converted to "^13" since "^p" can't actually be used
'in wildcard searches.
'
'Author: Peter Straton
'
'*************************************************************************************************************
Const PgraphMark As String = "^p"
Const ASCII_CR As String = "^13"
Dim FindRange As Range
If IsMissing(DocRange) Then
ThisDocument.Activate 'In case it isn't
Set DocRange = ActiveDocument.Range
End If
Set FindRange = DocRange
LinePattern = Replace(LinePattern, PgraphMark, ASCII_CR, , , vbTextCompare)
If Left(LinePattern, 3) <> ASCII_CR Then LinePattern = ASCII_CR & LinePattern
If Right(LinePattern, 3) <> ASCII_CR Then LinePattern = LinePattern & ASCII_CR
With FindRange.Find 'Set basic Find parameters...
.ClearFormatting
.Wrap = wdFindStop
.Format = False
.Forward = True
.MatchCase = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.MatchWholeWord = False
End With
'BUG WORKAROUND - BUG WORKAROUND - BUG WORKAROUND - BUG WORKAROUND - BUG WORKAROUND - BUG WORKAROUND - BUG
'
'For reasons that are not understood (apparently by anyone), under some circumstances that are difficult to
'characterize, Word's Find/Replace tool can cause Word to freeze after replacing whole lines (^p to ^p) with
'single or multiple ^p characters. Consequently, the found ranges must be explicitly deleted instead of
'replaced.
Do 'Find each instance of LinePattern and delete them
With FindRange.Find
If Not .Execute(FindText:=LinePattern, MatchWildcards:=True) Then Exit Do 'Must assert MatchWildcards
'each time!
With FindRange
.SetRange Start:=.Start, End:=.End - 1 'Leave trailing ^p
End With
' FindRange.Select 'Debug
FindRange.Delete
Set FindRange = DocRange 'Re-establish find-range as the *now-modified* document range
End With
Loop
'END WORKAROUND - END WORKAROUND - END WORKAROUND - END WORKAROUND - END WORKAROUND - END WORKAROUND - END
End Sub