Need Help with Deleting Empty Paragraphs in Word 2003

I have written the code below to delete all empty paragraphs at the end of a 
document and then place the cursor at the end of the last paragraph. 

It works fine as a stand alone sub in a new doc, but fails inside the real 
document that contains other code that manipulates several documents. 

The failure is that it will delete the last empty para, but then gets stuck 
looping inside the While...Wend because subsequent .Delete are not happening. 
So, the question is why would this work in one document, but then fail in 
another? 

       n = 0                                                       
        With ActiveDocument
            oNumPara = .Paragraphs.Count
            Debug.Print "Original number of paragraphs: " & oNumPara
            While .Paragraphs.Last.Range.Words.Count = 1
                Debug.Print ActiveDocument
                Debug.Print .Paragraphs.Last.Range.Words.Count
                .Paragraphs.Last.Range.Delete
                n = n + 1
            Wend
            .Paragraphs.Last.Range.Select
            Selection.Collapse direction:=wdCollapseEnd
            fNumPara = .Paragraphs.Count
                Debug.Print n & " empty paragraphs were removed"
                Debug.Print "Final number of paragraphs: " & fNumPara
        End With

Thanks very much.
JohnS
0
Utf
11/13/2009 9:20:05 PM
word.vba.general 1023 articles. 1 followers. Follow

4 Replies
1529 Views

Similar Articles

[PageSpeed] 46

Why don't you just use a Wildcard Replace to replace [^13]{2,} with nothing.

-- 
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"jjfeenix" <jjfeenix@discussions.microsoft.com> wrote in message 
news:41C52188-4F2D-4595-8D0B-70518C56DF27@microsoft.com...
> I have written the code below to delete all empty paragraphs at the end of 
> a
> document and then place the cursor at the end of the last paragraph.
>
> It works fine as a stand alone sub in a new doc, but fails inside the real
> document that contains other code that manipulates several documents.
>
> The failure is that it will delete the last empty para, but then gets 
> stuck
> looping inside the While...Wend because subsequent .Delete are not 
> happening.
> So, the question is why would this work in one document, but then fail in
> another?
>
>       n = 0
>        With ActiveDocument
>            oNumPara = .Paragraphs.Count
>            Debug.Print "Original number of paragraphs: " & oNumPara
>            While .Paragraphs.Last.Range.Words.Count = 1
>                Debug.Print ActiveDocument
>                Debug.Print .Paragraphs.Last.Range.Words.Count
>                .Paragraphs.Last.Range.Delete
>                n = n + 1
>            Wend
>            .Paragraphs.Last.Range.Select
>            Selection.Collapse direction:=wdCollapseEnd
>            fNumPara = .Paragraphs.Count
>                Debug.Print n & " empty paragraphs were removed"
>                Debug.Print "Final number of paragraphs: " & fNumPara
>        End With
>
> Thanks very much.
> JohnS 

0
Doug
11/13/2009 9:58:16 PM
Doug:

> Why don't you just use a Wildcard Replace to replace [^13]{2,} with nothing.

Thanks very much for the suggestion. I'm not exactly sure how to implement 
it. The task is to only remove empty paragraphs at the end of the document, 
nowhere else in the document. That's why the routine starts at the end and 
works backwards. Can this be done with the Wildcard Replace?

Besides cracking this nut another way, I still need to know what is keeping 
the routine from working properly when included in a larger program, but 
perfectly by itself. Any suggestions about that?

Thanks,
JohnS
0
Utf
11/15/2009 2:06:01 PM
I would use

Dim i As Long
With ActiveDocument
    For i = .Paragraphs.Count To 1 Step -1
        If .Paragraphs(i).Range.Words.Count < 2 Then
            .Paragraphs(i).Range.Delete
        Else
            Exit Sub
        End If
    Next i
End With


-- 
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"jjfeenix" <jjfeenix@discussions.microsoft.com> wrote in message 
news:B2F87497-73B4-4B14-B4FA-D767D922AFED@microsoft.com...
> Doug:
>
>> Why don't you just use a Wildcard Replace to replace [^13]{2,} with 
>> nothing.
>
> Thanks very much for the suggestion. I'm not exactly sure how to implement
> it. The task is to only remove empty paragraphs at the end of the 
> document,
> nowhere else in the document. That's why the routine starts at the end and
> works backwards. Can this be done with the Wildcard Replace?
>
> Besides cracking this nut another way, I still need to know what is 
> keeping
> the routine from working properly when included in a larger program, but
> perfectly by itself. Any suggestions about that?
>
> Thanks,
> JohnS 

0
Doug
11/15/2009 7:26:06 PM
Doug:

Thanks for the code. I will try it later today and let you know how it works.

> I would use
> 
> Dim i As Long
> With ActiveDocument
>     For i = .Paragraphs.Count To 1 Step -1
>         If .Paragraphs(i).Range.Words.Count < 2 Then
>             .Paragraphs(i).Range.Delete
>         Else
>             Exit Sub
>         End If
>     Next i
> End With

Thanks,
JohnS 

0
Utf
11/16/2009 2:56:07 PM
Reply:

Similar Artilces:

Unable to open .docx documents in Word 2002
I cannot open word documents sent with .docx extendion in word 2002. I checked my language setting and it is English. What is wrong? Download the Compatibility Pack. http://www.microsoft.com/downloads/details.aspx?familyid=941b3470-3ae9-4aee-8f43-c6bb74cd1466&displaylang=en -- JoAnn Paules MVP Microsoft [Publisher] Tech Editor for "Microsoft Publisher 2007 For Dummies" "Johnpm" <Johnpm@discussions.microsoft.com> wrote in message news:43E248E8-5D83-4E91-9743-CE12CCD6A443@microsoft.com... >I cannot open word documents sent with .docx extendi...

From Outlook 2000 to Outllook 2003
How do I migrate I personal folders file (.pst) from Outlook 2000 to Outlook 2003? Read the Help Files: http://office.microsoft.com/en-us/assistance/HA010771141033.aspx -- Russ Valentine [MVP-Outlook] "rolo" <rolo@discussions.microsoft.com> wrote in message news:706405A0-2971-409F-B213-67714B12713C@microsoft.com... > How do I migrate I personal folders file (.pst) from Outlook 2000 to > Outlook > 2003? Thanks Russ it helped. By the way how can I get to this useful help files? "Russ Valentine [MVP-Outlook]" wrote: > Read the Help Files: > htt...

Need Help, Task Start Date is wrong
I’m using MS Project 2007, have several task linked with finish to start. I have set date to schedule from, hours per day set to 8 and Working Monday thru Friday. My schedule shows Task 1 Duration 4 days, start Wed 6/2/10, Finish Mon 6/7/10 Task 2 Duration 3 days, start Mon 6/7/10, Finish Thu 6/10/10 Task 2 should have a Start Date of 6/8/10 not 6/7/10; what is causing this? Thanks in advance for your help. ...

How2 set Defaults for Paragraphs in Pub2003
I have just upgraded to Pub2003. My old Pub2000 had no such feature. My problem is when I cut and paste into the new Pub2003 it automaticly uses a Line Spacing default "After Paragraphs: 9pt" - this whacks out all my old logos and forms when they are copy/pasted in to a new Pub2003 .pub If I open an old (2k) .pub file with Pub2003 the spacing is fine - if figures out the Line Spacing is "After Paragraphs: 0pt" ...

Unable to delete empty database
I recently moved a group of users out of one database into another Database within the same storage group. After successfully moving all users (except SMTP and SystemMailbox) out of the 1st database I attempted to delete the database only to receive an error: One or more users currently use this mailbox store. These users must be moved to a different mailbox store or be mail disabled before deleting this store. Id no: c1034a7f Exchange System Manager Can I delete this mailbox via ADSIEDIT? Is this the recommended alternative? Thanks, BJ Use the AD Users and Computers snap-in ...

Looking for Excel Help
I'm a very novice Excel user and am looking for a little help with creating a formula for a spreadsheet I'm creating for my personal use. I would appreciate some assistance if possible. Thanks in advance. Dan --- Message posted from http://www.ExcelForum.com/ Hi Dan! Post a sample of what you want to do. Your question is just a tad open ended <g> -- Regards Norman Harker MVP (Excel) Sydney, Australia njharker@optusnet.com.au Excel and Word Function Lists (Classifications, Syntax and Arguments) available free to good homes. "DanB4105" <DanB4105.ywtpa@excelfor...

New to excel
Hi All, I'm new to Excel ( and to this forum :) ) and so I hope somebody may b able to help me. I've got 2 questions.... QUESTION 1 I've got a spreadsheet which takes data from one worksheet and uses i to calculate data in a second worksheet using the following code formula: =IF('4th November 2005'!B19="","nothing here dude",IF(B19<'4th Novembe 2005'!B19,"UP",IF(B19='4th November 2005'!B19,"Same",IF(B19>'4t November 2005'!B19,"DOWN")))) The problem is, when I create a new worksheet I have...

setup Windows Mail as Word 2003 default emailer
All I can do is setup Outlook. I do not use Outlook. I would like to email Word docs using MS Windows Mail (new version of Express) In the Windows Start area, type Regedit into the search bar and then start the Registry Editor and go to HKEY_CURRENT_USER>Software>Clients>Mail Right Click on the (Default) item and then on Modify and in the Value data: field enter Windows Mail so that after you click OK, you have (Default) REG_SZ WIndows Mail -- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a pa...

Filters not working in Exchange 2003
I have been trying to turn on the Recipient, Connection, and Sender filters. I have gone to the Default SMTP Virtual Server and turned it on there without getting an error but when I go to the Properties and add senders to block and the hit Apply, it tells me that I must manually turn the filtering on in the SMTP VS. I have stopped and started the Default SMTP VS but still no luck. Any ideas? Hi Wayne That is a standard dialog box, it does not check to see if it is already enabled, have you tested the sender filtering? -- Mark Fugatt Microsoft Limited This posting is provided &quo...

will CRM load on a 2003 server?
will CRM load on a 2003 server? Microsoft CRM v1.2 supports Windows 2000/2003 Server. Frank Lee Workopia, Inc. >> Other Microsoft CRM Online Forum Resources: http://www.workopia.com/Links.htm >-----Original Message----- >will CRM load on a 2003 server? >. > No problem. We just completed a 1.2 installation on a 2003 server, without any problems. Brian Demoe "Troy Hicks" <tlhicks@nc.rr.com> wrote in message news:03dd01c3dcb2$93653a00$a501280a@phx.gbl... > will CRM load on a 2003 server? CRM 1.2 will also load on Small business server 2003 as wel...

How to delete a set of rows depending on Value
I have two worksheets Worksheet A 27 Columns 1600 Rows. Worksheet B 1 Column 200 Rows I need Worksheet A to look at each cell in Worksheet B, if a cell exists in Worksheet A, then I need the row deleted... Basically I have a list of Grand list of items to do, then a list of items already completed. I need to now remove all entries in the grand list that have been completed. Is this feasible or should I look at using some Unix script. It sounds like you could use VLOOKUP to find out if the value in B exists on A: http://www.officearticles.com/excel/vlookup_formulas_in_microsoft_excel....

Please help #8
I have Exchange 2000 and Outlook 2003 in Cache mode. Has anyone seen this or know how to fix it? Task 'Microsoft Exchange Server' reported error (0x8007000E) : 'Out of memory or system resources. Close some windows or programs and try again.' "John S" wrote: > > I have Exchange 2000 and Outlook 2003 in Cache mode. Has anyone seen this or > know how to fix it? > > Task 'Microsoft Exchange Server' reported error (0x8007000E) : 'Out of > memory or system resources. Close some windows or programs and try again.' > >...

Outlook 2003 Drag and Drop Emails
I have an issue where there is a SBS 2003 server (newly installed) & when I drag emails to the file system (explorer window) in order to create file records of the emails it generates an error. Dialog Box Name: Error Copying File or Folder Error Msg: Not enough storage is available to process this command. I can't find an error logged anywhere, either on the server event logs or on the local machine event logs... I have searched the MS KB & Office online, but no joy yet... If anyone can help that would be great!!! R ...

I need a macro
I would like a macro to do the following: I would place the the cursor in any cell and this macro would give me the average of all the entries to the left if the selected cell. EX: I place the cursor in cell M12. I want this macro on display in cell M12 the average of all the values from Col C12 to Col L12. Note that there may NOT be entries in all the cells in that range. Also the start point would always be col C .Thanks You can do this easily with a worksheet_selection event in the sheet module but you would probably want to restrict to a certain range or columns and rows or it woul...

Redirect Exchange 2000 IS backup to different Exchange 2003 server
I recently added an Exchange 2003 server to the same org as a 2000 server. I have dbs from the 2000 server that I need to restore to retrieve email from a user whose mailbox was moved to 2003. So I need to restore the db for that mailbox from BEFORE it was moved because when you move mailboxes you lose any deleted items that were being saved by retention policy. Is this possible? I'm using Veritas Backup Exec 10 but nothing in their support KB seems to follow this exact scenario. If it helps, the old Exch 2000 server is currently empty of users and is ready to be uninstalled. W...

unable to paste Excel 2003 chart into Outlook 2003
(This was posted on "excel.charting" group.) I have a user who's unable to paste an Excel 2003 chart into Outlook 2003 email message. In Outlook options, the checkbox is selected for "Use Microsoft Office Word 2003 to edit e-mail messages". When I tested this on my own computer running the same version of Office, if the box is check, I have no problem pasting; if this box is cleared, I cannot paste. But on his computer, it doesn't work regardless. Thanks and regards, TL ...

How do I Remove a Split from my Comments in Excel 2003? #2
I have set my current workbook to split/freeze the first column and first 2 rows. Now, when I add a comment to the second row (in any column) my comments are cut off if I should scroll down. I don't ever remember the behavior before. And I don't know what I've done to enable it but it's really annoying. How do turn this off ? ...

A message that cannot be deleted, opend or moved
THere is a message in a user's inbox that cannot be opened, deleted or moved. How can I delete that message ? Amila Amila Chandrasekera <amilanc@spss.com> wrote: > THere is a message in a user's inbox that cannot be opened, deleted or > moved. How can I delete that message ? One drastic way is to create a new PST file and copy everything from the old file to the new, with the exception of the misbehaving message. Then remove the old file. did you try shift-delete? -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Coauthor, OneNote ...

Help with importing data
Can I have users fill in a form in Access and have that data be transferred and updated to a spreadsheet. Need for fill out several fields and then export to a specific spreadsheet and place that data into the cells that will update that cell (add to the total in that cell) of a spreadsheet. ...

MOVE TO FOLDER... only appears. I need MOVE TO FOLDER
This is very odd and I've found that I've had this problem before with not finding icons. Some I've found at the office don't appear here and vice versa, or they act differently. I have Outlook 2000 in both places! Very odd. At home, I couldn't find the HIGH IMPORTANCE icon under the list of items available in the customize. Yet I have it at work. That's the one where when the HIGH IMPORTANCE is on, it shows a depressed button state. I really need that in both places. Anyway, simple (or so I thought) - I ended up just brining the toolbar from work on a floppy, a...

How to get to email from Word
Sorry to break the thread, but replying to the original thread now produces an error message :( The suggested changes don't make any difference to the essential premise that it all works as long as Outlook is running in the background. Stepping though the code has provided inconsistent results. It usually crashes at the line strEMail = Application.GetAddress("", strEMail, False, 1, , , True, True) when the address is selected from the dialog and you OK out of the dialog; but I have noticed that this call has produced crashes in other routines where Outlook is not running. Th...

Recreating site/directory connectors on 2003?
Just got the first 2003 server up and running. We have a single domain, and two sites/admin groups. SiteA contains the current 5.5 server (ServerA) and the new 2003 server (ServerB). SiteB contains a single 5.5 server (ServerC). There is a site connector created under 5.5 between SiteA and SiteB with ServerA and ServerC as the bridgeheads in each site. There is also a directory replication connector between SiteA and SiteB, again with ServerA and ServerC as the bridgeheads in each site. The ADC is installed and working with the default mailbox/public folder CA's for both SiteA ...

Cannot Delete Any Records
We're running CRM 3 Rollup v3 and we have recently found that we cannot delete any records in CRM. We recently migrated over to a Windows 2003 Enterprise install running SQL 2000 Enterprise. Everything else is working without issue for us. For example I am a Sys Admin, and I receive a generic SQL Server error when going to delete an activity? I know that in the past this was possible, any thoughts? Sean; sounds like something went wrong with your migration. you can run a CRM trace or a SQL Profiler trace while performing the deletion to see if you can pin-point the error. Dave Ire...

Nothing happens at ctrl-alt-delete prompt
I have a desktop running WinXP Pro. It was running terribly slow, so I rebooted. When I got to the ctrl-alt-delete prompt, I hit the keys and nothing happened. I powered off and back on again and same thing. I rebooted to Last Known Good Configuration, and still the same thing. Then I tried a different keyboard. Still nothing happens when you press ctrl-atl-delete. During the boot process, the lights on the keyboard (num lock, cap lock, etc) blink, so it appears that the keyboard and computer are communicating on some level, at least. Anyone have any thoughts on anything ...

Are Exchange 2003 OWA Backups necessary
Is there a real need to backup OWA with the DR option available with Exchange 2003? There are no stores running on this box. -- Thanks Paul Paul, I am not sure I understand the question. OWA is just away of accessing your mailbox via a web browser, so by backing up the Exchange servers hosting the mailboxes you are backing up what you can see ia OWA. When you state that "there are no stores running on this box", what box are you reffering to ? Is it a front-end server ? Regards Paul Ford Edge IT Ltd "Paul Bergson" <pbergson@allete_nospam.com> wrote in...