+ Reply to Thread
Results 1 to 3 of 3

Removing paragraphs length 5 from word document usin VBA

  1. #1
    Registered User
    Join Date
    09-13-2016
    Location
    Cambridge
    MS-Off Ver
    2003
    Posts
    12

    Removing paragraphs length 5 from word document usin VBA

    I have put together this code to remove blanks lines and it works.
    But it remove all blanks line
    What I want to do is remove a blank line if there are block of 5 blanks lines
    If I change the value If Len(oPara.Range.Text) = 5 Then the VBA macro fails.
    It also fails if I set it to 2...

    Any suggestions???

    Thanks



    Sub Pararemove()

    Dim sPath As String
    Dim WdDoc As Document, sFile As String
    Dim sCellText As String
    Dim strFilename As String
    Dim oPara As Paragraph


    Application.ScreenUpdating = False
    Application.DisplayAlerts = False


    'Change the path below to suit your need
    sPath = "\c:Deanj\Academy_Test - Copy1\"

    sFile = Dir(sPath & "*.doc")

    'Loop through all .doc files in that path
    Do While sFile <> ""
    Set WdDoc = Application.Documents.Open(sPath & sFile)
    For Each oPara In ActiveDocument.Paragraphs
    If Len(oPara.Range.Text) = 1 Then
    oPara.Range.Delete
    End If
    Next
    If ActiveDocument.Saved = False Then ActiveDocument.Save
    sFile = Dir
    Loop

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

    End Sub

  2. #2
    Forum Expert
    Join Date
    03-20-2015
    Location
    Primarily UK, sometimes NL (COVID allowing)
    MS-Off Ver
    Work: Office 365 / Home: Office 2010
    Posts
    2,402

    Re: Removing paragraphs length 5 from word document usin VBA

    Have you tried just doing a find and replace for 5 paragraph returns instead? Not sure what that would be in VBA, but a quick 'record macro' should give something which could be tweaked.
    Regards,
    Aardigspook

    Recently moved house, internationally, during COVID (!) so am a bit busy and may not reply quickly. Sorry - it's not personal - I will reply eventually.
    If your problem is solved, please go to 'Thread Tools' above your first post and 'Mark this Thread as Solved'.
    If you use commas as your decimal separator (1,23 instead of 1.23) then please replace commas with semi-colons in your formulae.
    You don't need to give me rep if I helped, but a thank-you is nice.

  3. #3
    Forum Guru macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,667

    Re: Removing paragraphs length 5 from word document usin VBA

    Quote Originally Posted by Aardigspook View Post
    Have you tried just doing a find and replace
    Indeed. A single wildcard Find/Replace is probably all that's needed. For example:
    Find = ^13[ ^t^s^13]{1,}
    Replace = ^p
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. add Paragraphs in cell table in word
    By cvelle89 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-12-2015, 05:38 AM
  2. Delete all paragraphs containing a particular image using a VBA macro in MS Word
    By sshishirkumar in forum Word Programming / VBA / Macros
    Replies: 1
    Last Post: 09-30-2013, 07:06 PM
  3. Add text to Existing Word Document if it Doesn't exist create Word Document
    By unstable81 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-10-2013, 12:15 AM
  4. Inserting Frequently Used Paragraphs in to a document
    By tannersarms in forum Word Programming / VBA / Macros
    Replies: 1
    Last Post: 01-02-2009, 03:44 PM
  5. Moving paragraphs from MS Word to Excel
    By jrew23@yahoo.com in forum Excel General
    Replies: 3
    Last Post: 03-01-2005, 11:08 PM
  6. How to select the first 5 paragraphs in a Word Document from Excel
    By quartz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-12-2005, 12:06 PM
  7. [SOLVED] Display Word Paragraphs in Excel???
    By quartz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-10-2005, 04:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1