Share via

vba to delete empty paragraph

Anonymous
2017-08-21T23:24:23+00:00

Hi, I have a macro that includes a search on the word "Description" in my document and my code works exactly like I planned (maybe clumsy) as long as there isn't an empty paragraph prior to the word Description.  If there is an empty paragraph prior to the word then I don't get the right result.  Could I get some help writing the search code where if an empty paragraph exists prior to the word Description the empty paragraph is deleted?  Here's what I have so far.

'remove empty paragraphs and bar code table

Selection.Find.ClearFormatting

    With Selection.Find

        .Text = "Description"

        .Replacement.Text = ""

        .Forward = True

        .Wrap = wdFindContinue

        .Format = False

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

thanks,

Cindy

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Paul Edstein 82,861 Reputation points Volunteer Moderator
2017-08-24T23:44:58+00:00

There are many books on VBA programming and there are on-line guides as well. You could also visit forums such as:

http://www.msofficeforums.com/word-vba/

https://www.excelforum.com/word-programming-vba-macros/

http://www.vbaexpress.com/forum/forumdisplay.php?20-Word-Help

https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev

where, even without joining up, you can look through the threads for topics you're interested in and see how some of the more advanced users address different coding issues.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

Paul Edstein 82,861 Reputation points Volunteer Moderator
2017-08-22T22:33:58+00:00

The code I posted doesn't change your 'selection'. As this is obviously part of a larger project, some other aspect of your code is doing that.

Do note that you generally should not have to select anything with a macro. Doing so is inefficient, creates a lot of screen flicker and requires the code to constantly keep track of where the 'selection' might be. For example, the following code deletes the entire first Section and the 'Description' without anything ever being selected:

Sub Demo()

Application.ScreenUpdating = False

With ActiveDocument.Range

  .Sections.First.Range.Delete

  With .Find

    .ClearFormatting

    .Replacement.ClearFormatting

    .Forward = True

    .Wrap = wdFindContinue

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchWildcards = False

    .MatchSoundsLike = False

    .MatchAllWordForms = False

    .Text = "^pDescription"

    .Replacement.Text = ""

    .Execute Replace:=wdReplaceAll

    .Text = "Description"

    .Replacement.Text = ""

    .Execute Replace:=wdReplaceAll

  End With

End With

Application.ScreenUpdating = True

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

Paul Edstein 82,861 Reputation points Volunteer Moderator
2017-08-22T13:45:58+00:00

Try:

With ActiveDocument.Range

  With .Find

    .ClearFormatting

    .Replacement.ClearFormatting

    .Forward = True

    .Wrap = wdFindContinue

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchWildcards = False

    .MatchSoundsLike = False

    .MatchAllWordForms = False

    .Text = "^pDescription"

    .Replacement.Text = ""

    .Execute Replace:=wdReplaceAll

    .Text = "Description"

    .Replacement.Text = ""

    .Execute Replace:=wdReplaceAll

  End With

End With

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

11 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-08-22T13:27:21+00:00

    I would need that marker deleted.  The macro removes what's on the 1st page, deletes the section break between the 1st and 2nd page and that moves Description from the 2nd page up to the first page.  Then the header is removed and a new one copied in from a template.  Then two markers are placed at the top of the page with each one holding a bookmark that locks down the header.  It works fine for those documents that don't have that extra marker on the 2nd page.

    Was this answer helpful?

    0 comments No comments
  2. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2017-08-21T23:50:55+00:00

    What would the right result be when there is an empty paragraph before 'Description'?

    Was this answer helpful?

    0 comments No comments