Results 1 to 11 of 11
  1. #1
    2 Star Lounger
    Join Date
    Mar 2001
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Remove whole paragraphs

    I have an html document that I want to remove certain recurring lines of code from. I opened the document in Word as a text file. I can isolate the material I want to remove as discrete paragraphs, but the text in each is not exactly the same (the code is for adding catalog items to a shopping cart). Using Find/Replace, I would like to be able delete all of these
    paragraphs. The only information I have been able to find on this subject says to find paragraphs by thir formatting (style) and then replace them with nothing, i.e., eliminate them. But these paragraphs are not characterized by any particular style. Each paragraph begins with the same word. Is there a way to get Word to delete every paragraph that begins with a certain word?
    Clark

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Remove whole paragraphs

    Hi Clark,

    The following macro should do this for you.
    Where I've put "Specific" you should substitute whatever word it is that begins your to-be-deleted paragraphs. Also, replace the "8" in the code with the number of letters in your specific search word.

    To use this preformatted code, select it here, copy, then paste it into a regular Word document. Then select it in the Word document, copy again, and then you can paste it into the VB Editor.

    It might also be possible to do this with a search and replace using wildcards, but I'll leave it to one of the 'wildcard wildcats' here to provide details on that method. Anyway:

    <pre>Sub DeleteSpecificParagraphs()
    Dim n As Long
    For n = ActiveDocument.Paragraphs.Count To 1 Step -1
    If Left(ActiveDocument.Paragraphs(n).Range.Text, 8) = "Specific" Then
    ActiveDocument.Paragraphs(n).Range.Delete
    End If
    Next n
    End Sub
    </pre>

    Gary

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Remove whole paragraphs

    Hi CKenyon:

    Just for completeness, there's a way to do this in Find/Replace also.

    Use Wildcards:
    Find: ^13specific start word*^13
    Replace: ^p

    This says to find a paragraph mark, followed by your certain word, followed by anything, & then another paragraph mark. It leaves only 1 paragraph mark, thus deleting the paragraph.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Remove whole paragraphs

    I couldn't resist:


    dim strSpec as string
    strSpec = "Specific"
    dim intLen as integer
    intLen=len(strSpec)
    Left(ActiveDocument.Paragraphs(n).Range.Text, IntLen) = "Specific"


    or even:
    public const strcSpec as string = "Specific"
    which is, admittedly less fun ...

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Remove whole paragraphs

    <g>:

    <pre>Sub DeleteSpecificParagraphs()
    Dim strSpec As String
    Dim n As Long
    Dim intLen As Integer
    Dim objDocParas As Paragraphs
    Set objDocParas = ActiveDocument.Paragraphs
    strSpec = "Specific"
    intLen = Len(strSpec)
    With objDocParas
    For n = .Count To 1 Step -1
    If Left(.Item(n).Range.Text, intLen) = "Specific" Then
    .Item(n).Range.Delete
    End If
    Next n
    End With
    End Sub
    </pre>


  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Remove whole paragraphs

    Nice!

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Remove whole paragraphs

    Gary

    I can see we will have to start censoring your reading of Chris' code as you've started overdefining the life out of your macros. <img src=/S/ouch.gif border=0 alt=ouch width=15 height=15>
    <pre>Sub DeleteSpecificParagraphs()
    Dim strSpec As String
    Dim n As Long
    strSpec = "Specific"
    With ActiveDocument.Paragraphs
    For n = .Count To 1 Step -1
    If Left(.Item(n).Range.Text, Len(strSpec)) = strSpec Then .Item(n).Range.Delete
    Next n
    End With
    End Sub</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Not that there's anything wrong with that

    <img src=/S/clown.gif border=0 alt=clown width=15 height=15> <img src=/S/kiss.gif border=0 alt=kiss width=15 height=15> <img src=/S/wink.gif border=0 alt=wink width=15 height=15>
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. #9
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Remove whole paragraphs

    Yer right I'm being overly "specific".

    Here's a version with some more functionality and a user interface:

    <pre>Option Explicit
    Public lngDelParaCt As Long
    '==========================
    Sub CallDeleteParaFunction()
    Dim strSel As String
    Dim strInput As String
    strSel = Selection.Text
    strInput = InputBox(Prompt:="Click OK to search for the selected word" _
    & " or type a different one.", Default:=strSel)
    If DeletedSpecificParas(strInput) Then
    MsgBox "Search string was found and " & lngDelParaCt & " paragraphs were deleted."
    Else
    MsgBox "No matching paragraphs were found."
    End If
    End Sub
    '==========================
    Public Function DeletedSpecificParas(strSpec As String) As Boolean
    Dim n As Long
    Dim intLen As Integer
    Dim lngBefParaCt As Long
    Dim lngAftParaCt As Long
    Dim objDocParas As Paragraphs
    Set objDocParas = ActiveDocument.Paragraphs
    intLen = Len(strSpec)
    With objDocParas
    lngBefParaCt = .Count
    For n = .Count To 1 Step -1
    If Left(.Item(n).Range.Text, intLen) = strSpec Then .Item(n).Range.Delete
    Next n
    lngAftParaCt = .Count
    If lngAftParaCt <> lngBefParaCt Then
    DeletedSpecificParas = True
    lngDelParaCt = lngBefParaCt - lngAftParaCt
    Else
    DeletedSpecificParas = False
    End If
    End With
    End Function
    </pre>


  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Remove whole paragraphs

    > a user interface:

    But not a REAL user interface:

    <pre>Public Sub cmd_ColoursClick(frmMe As UserForm)
    ' Procedure : cmd_ColoursClick
    ' Description: Change the colors on all buttons to a new standard combination.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: A Word97 GUI form.
    ' Returns: None.
    ' Assumes: None.
    ' Side Effects: None.
    ' Tested: By a call from the user.
    ' Re-seed the randomizer (probably from the system clock)
    Randomize
    Dim strColours As String ' Will accumulate a new string of colours. See cmd_UserFormInitialize
    ' We start with a null, the delimiter character, the base (usually 256) and another delimiter.
    strColours = str(0) & strcINIFileDelimiter & str(intcBase) & strcINIFileDelimiter
    ' Values for Background Colour Red (Blue and Green)
    Dim intBCR As Integer, intBCB As Integer, intBCG As Integer
    ' Background RED code
    While InStr(strColours, str(intBCR)) ' loop until a unique colour is made.
    intBCR = Rnd * intcBase ' generate a fresh colour.
    Wend
    strColours = strColours & str(intBCR) & strcINIFileDelimiter ' append to colours string
    ' Background BLUE code
    While InStr(strColours, str(intBCB)) ' loop until a unique colour is made.
    intBCB = Rnd * intcBase ' generate a fresh colour.
    Wend
    strColours = strColours & str(intBCB) & strcINIFileDelimiter ' append to colours string
    ' Background GREEN code
    While InStr(strColours, str(intBCG)) ' loop until a unique colour is made.
    intBCG = Rnd * intcBase ' generate a fresh colour.
    Wend
    strColours = strColours & str(intBCG) & strcINIFileDelimiter ' append to colours string
    ' Values for Foreground Colour Red (Blue and Green)
    Dim intFCR As Integer, intFCB As Integer, intFCG As Integer
    ' Foreground RED code
    intFCR = Rnd * intcBase ' generate a fresh colour.
    strColours = strColours & str(intFCR) & strcINIFileDelimiter ' append to colours string
    ' Foreground BLUE code
    intFCB = Rnd * intcBase ' generate a fresh colour.
    strColours = strColours & str(intFCB) & strcINIFileDelimiter ' append to colours string
    ' Foreground GREEN code
    intFCG = Rnd * intcBase ' generate a fresh colour.
    strColours = strColours & str(intFCG) & strcINIFileDelimiter ' append to colours string
    ' Now make use of the colours to set the Background and Foreground colours.
    Dim myControl As Control
    For Each myControl In frmMe.Controls
    On Error Resume Next
    myControl.BackColor = RGB(intBCR, intBCB, intBCG)
    On Error Resume Next
    myControl.ForeColor = RGB(intFCR, intFCB, intFCG)
    Next myControl
    ' Values for Background Screen Red (Blue and Green)
    Dim intBSR As Integer, intBSB As Integer, intBSG As Integer
    While InStr(strColours, str(intBSR)) ' loop until a unique colour is made.
    intBSR = Rnd * intcBase ' generate a fresh colour.
    Wend
    strColours = strColours & str(intBSR) & strcINIFileDelimiter ' append to colours string
    While InStr(strColours, str(intBSB)) ' loop until a unique colour is made.
    intBSB = Rnd * intcBase ' generate a fresh colour.
    Wend
    strColours = strColours & str(intBSB) & strcINIFileDelimiter ' append to colours string
    While InStr(strColours, str(intBSG)) ' loop until a unique colour is made.
    intBSG = Rnd * intcBase ' generate a fresh colour.
    Wend
    strColours = strColours & str(intBSG) & strcINIFileDelimiter ' append to colours string
    ' Now make use of the colours to set the Form colours.
    frmMe.BackColor = RGB(intBSR, intBSB, intBSG)
    ' Preserve these settings in the application INI file
    Call strPP(strcApplication, strcApplication, strcColours, strColours)
    ' We no longer trace/log the colour change
    ' Call ERL("0013", intcErrorInform, strColours)
    ' I don't know why I repaint at this point.
    frmMe.Repaint
    End Sub
    </pre>


  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Remove whole paragraphs

    That ain't an interface, that's an "in yer face"! <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

    You could market this as "Chris' Liquid GUI" <img src=/S/devil.gif border=0 alt=devil width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •