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
1419 Views

Similar Articles

[PageSpeed] 58

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:

Need help ASAP!!
I've just been on the already installed version of windows mail on my laptop and i already have an email address and can't get it onto the windows mail. It only gives you the option of a new account. Please reply!! -- Im a cod fan haha lol If you have never set up your email account in that copy of Windows Mail, then as far as Windows Mail is concerned, it is a new account. Simply follow the prompts after going to Tools, Accounts, Add, E-mail... Gary VanderMolen, Microsoft MVP (Mail) "STaTiC_BLuE" wrote in message news:B5201BC2-0822-4456-B08B-86787DD8D736@...

Need macro to move to first blank cell in column
Hi. I am trying to use a macro to move from A1 to the first blank cell in col A. There are no gaps in the data. When the first blank cell is selected, data will be copied and pasted from another worksheet. Then the adjacent cell in col B must be selected and again data will be copied and pasted there from another worksheet. Finally, the adjacent cell in col C must be selected but the formula from the cell above must be extended down one cell. I have recorded a macro which does all this but the problem is that when I repeat the macro the same row is always selected instead of the ...

Formula help... #3
In column A there are cells with team names in them like the following: NYY NYY NYY @OAK @OAK @OAK BOS BOS BOS @TEX @TEX @TEX I would like column B to list all the team names without the @ symbol and column H to list all the teams with the @ symbol. For the example I would like column B to have NYY NYY NYY BOS BOS BOS =COUNTIF(A:A,"@*") and =COUNTIF(A:A,"<>@*") -- HTH RP (remove nothere from the email address if mailing direct) "Jambruins" <Jambruins@discussions.microsoft.com> wrote in message news:135965C4-7523-47AB-88DE-D0B12B652CD5@m...

Emailing in excel 2003 02-26-10
If i type in the cell A34: neil.Holden@test.com and press a button is it possible to email to the address of what ever is in A34 is? The email body should say: this has been submitted for cell B34 and todays date. Thanks. Check out Ron De Bruins "Send-Mail" tips: http://www.rondebruin.nl/sendmail.htm Micky "Neil Holden" wrote: > If i type in the cell A34: neil.Holden@test.com and press a button is it > possible to email to the address of what ever is in A34 is? > > The email body should say: this has been submitted for cell B34 and...

i need an example of a hourly and weekely pay sheet
i need to create a paysheet that will allow me to pay my employees per hour. "azure" <azure@discussions.microsoft.com> wrote in message news:8DD3F54C-3077-4D5E-B4C4-011DE0F73901@microsoft.com... > i need to create a paysheet that will allow me to pay my employees per hour. So, what you need then is:- EmployeeName, HoursWorked, HourlyRate, TotalPay where TotalPay = HoursWorked*HourlyRate ...

Word Attachment problem
Have a network of PC's with MS Office of various flavours, drawing Email from Exchange Server. On one PC, have had an ongoing problem with Word attachments. PC has ben reformatted, permissions opened up to Admin, Office upgraded, from 2000 through XP to 2003 all with the same result. When you double click on a Word attachment and chosse to open it, you get a 'Serious error' report with the option to restart Word. Sometimes this works other times it just loops through the same. The latest update to Office 2003, when using the 'Send Report' and then using the diagnosis fro...

Frontpage 2003 or Publisher 2003 ?
Which one should I use? I am not familiar with Publisher. I am familiar with FrontPage. What kind of support is needed on the server side for a web ? cgi? etc. Any info or a link to some info appreciated. Hi Cracker Jacks (cjacks<nospamz)@verizon.net), in the newsgroups you posted: || Which one should I use? I am not familiar with Publisher. I am || familiar with FrontPage. What kind of support is needed on the || server side for a web ? cgi? etc. Any info or a link to some info || appreciated. I recommend FrontPage 2003. FrontPage 2003 doesn't REQUIRE FrontPage Server extensions,...

Illegal operation error while printing EXCEL or WORD Files
Hi, I am facing an illegal operation error when i try to print any file from excel (any no. of pages), this happens in stand alone printer as well as a networked printer. When we press the print button, it flashes this message, but still prints, but once the printing is completed, i will have to restart the PC. Due to this error other applications PRINTING also will NOT HAPPEN and the only way out is, restart the PC. This happens not only in EXCEL, it happens in all the MS applications (outlook, access, front page, powerpoint also). When I check the print manager (before restart),...

ActiveX Control in Word
Hello, I have a couple of issues with an activex control developed in MFC targeted for Office. In Powerpoint, when the user right clicks on my control and selects "Edit", my control starts receiving mouse and keyboard input, since it becomes active. However, in Word when the user selects Edit, the control does not recive any input. But if I click the design toggle button in the Control toolbar, the control goes active and recives input. I would like my control to be activated in Word the same way as it gets activated in Powerpoint, is this possible? The second issue is, when...

Remove "don't add space between paragraphs of the same style"?
I'm editing a document that was created by someone else. Somewhere along the way, under format/paragraph options, they clicked the box called "don't add space between paragraphs of the same style." I want to un-click it, but the box is greyed out (showing the checkmark there) and I can't un-click it...and it's screwing up my formatting! Does anyone know how to find this rogue command & remove it? This option is not available for individual paragraphs, only for styles. You must go to the Modify Style dialog to change it. -- Suzanne S. Barnhill M...

outlook 2000 help file
Seems after I did added SP3 from MS upgrade site my help from outlook has quit working. message is msohelp caused error in itss.dll. help works in ALL other programs including outlook address book. I'm running windows 98 SE, Office 2000 SB and IE 6. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.542 / Virus Database: 336 - Release Date: 11/18/03 ...

Excel Crash
I use Excel and Word 2003 using Windows NT. I've kept some files on a jump drive so I can work on them at home. I attempted to work on a Word documents which had an Excel worksheet inserted in it. I tried double clicking on the worksheet to edit it and Word and Excel shut down. Now when I attempt to open Excel at home it asks for my Office XP Professional installation cd. (I have Office XP at home with Windows XP). I'm having a hard time locating my original discs. Does anyone have any suggestions or experience anything like this? ...

Outlook 2003
Hello. I copied my PST file from my D: drive to my F: drive. I then went into "Data File Management" to get the new location recognized and to have all future mail sent to that location. After doing this, my custom settings, like new folders, date settings, columns, etc, are all back to their default settings. Is there any way I can get my custom settings working again on the F: drive? Thanks, Dan Could you explain yourself a bit more? What custom settings are you refering to? You mean views? And what new folder settings? Also post your version of Outlook. -- Robert Spar...

outlook 2003 memory leak? /messages display as"none" and eats up memory
Hi there; When a certian user opens outlook, and clicks on sort by size, half of her e-mails disappear, and "none" is displayed. She said this happened to her at home, except she was on her normal view which is sort by date. The only way that I can recreate the problem is to click on sort by size. If you click on one of the "none's" or even hover the mouse over it, the memory will spike up! I had the performance tab open while I was doing this, and the memory being used was read at 1.46 GB! And if I close outlook the memory drops down to normal. I took ...

deleting fonts
My computer uses Vista Home Premium. I just had to reset it to its original state. I'm trying to delete fonts, but don't have permission. I am the only user of this computer and its administrator. When I've gone into the Fonts folder Properties - Security tab, the option for Special permissions is grayed out. When I click the Advanced option ("For special permissions or advanced settings, I don't know what to do next. Can someone please walk me through how to get permission in the Fonts folder so I can delete fonts? Thanks Kelly On Wed, 6 Jan 2010 07:...

How can I instantly delete emails? I get a strikethrough + purge
...

Please Help
I keep getting this Problem Event Name: BlueScreen OS Version: 6.1.7600.2.0.0.256.48 Locale ID: 1033 Additional information about the problem: BCCode: 1a BCP1: 00005003 BCP2: 8A000000 BCP3: 00005BFD BCP4: B67AF009 OS Version: 6_1_7600 Service Pack: 0_0 Product: 256_1 Files that help describe the problem: C:\Windows\Minidump\122609-26379-01.dmp C:\Users\Johnbebe\AppData\Local\Temp\WER-38891-0.sysdata.xml Read our privacy statement online: http://go.microsoft.com/fwlink/?link...8&clcid=0x0409 If the online privacy statement is not available, please read our p...

Outlook 2003 Contact data
Just starting using Outlook 2003. When composing an email, when go to address the note, it appears that there are 2 Contact files - 1 is "blank" and the other has lots of my contacts. It is the latter contact file that I enabled to use as my address book. Initially address book comes up the first one and I get error message. Then I toggle to the second one and all the names are accessible. How do I get rid of the one that appears blank, so only one contact file appears when I go to address an email? Les <lester123_nospam@comcast.net> wrote: > When composing an e...

suggestion needed
Hi, i have an application that need to monitor file share access (client ip, requested file). this information is available in the windows2000/2003/xp computer management mmc applet under Computer Management -> System Tools -> Shared Folders -> Sessions / Open Files. is there a way to do this? Use the NetFileEnum() API to get the opened files list. -- Cheers Check Abdoul [VC++ MVP] ----------------------------------- "ziggy" <dont@send.mails> wrote in message news:u2jaDjPcFHA.3040@TK2MSFTNGP14.phx.gbl... > Hi, > > i have an application that nee...

Exchange Server 2003 Fails to Start after reboot
Since rebooting our Exchange 2003 server running on Windows 2003 server it fails to start the System Attendant and any further exchange services. The only way to logon to the server is boot in safe mode with networking. The server is a member server and is reporting the following errors EVENT 2114 MSExchange DS Access Process INETINFO.EXE (PID=1720). Topology Discovery failed, error 0x80040a02. EVENT 2102 Process MAD.EXE (PID=2176). All Domain Controller Servers in use are not responding: crstgs003.CRSTG.LOCAL crstgs004.CRSTG.LOCAL EVENT 2114 Process MAD.EXE (PID=2176). Topology Discovery ...

Help pulling "pingable" IP of local machine into a variable
Ok, here's my need. I am writing a script that does a few things, but I'm having trouble just pulling the "production" IP address into a variable, to then use in the rest of the script. I've see plenty of scripts to ping remote hosts, but that's not what I need. I've also worked up a few scripts on my own, but they all pull every IP on the machine, or require you to know the intenal ID of the NIC interface you need queried. The issue with this is that the ID is assigned randomly, and it's something you just have to know. I'm rather new at VBSc...

Help! #10
I seem to have lost the ability to open links to Internet Explorer directly from my e-mail. I don't know how this happened. When I click the Web address, the page doesn't open. How can I re-establish the connection between Microsoft Outlook (version 2002) and Internet Explorer? Thanks. Have you installed any service packs (SP)? (Open HELP menu on Outlook and look under ABOUT MICROSOFT OUTLOOK) What Internet browser are you using? What version including SP's? (Open Browser, Select HELP menu, ABOUT) Do you use Microsoft Office also? If so, what version? Do you get a...

Please Help With Formula
i currently have a formula which reads '1'!J9, the 1 refers to a tab o the bottom of the screen. When i highlight this box and drag th formula down it changes to '1'!J10. i want the J9 to stay the same bu the tab reference to increase by one. I can lock the J9 function b using $J$9 but i cannot increase the tab reference by one PLEASE HELP Also is there a way i can freeze a tab on the bottom of the screen s my main tab does not dissapear when i scroll along the rest of th tabs. I have 250 tabs on one sheet but i need to keep one tab in vie all the time Thank yo -- wether ----...

"Delete and block" used by mistake, not able to un-do
- By mistake I clicked the "delete and block" button for a specific message. - The sender then ended up in the "block-list". - I removed the sender from the blocked list (and also added him to the allowed-list) - However, I still do not manage receive messages from the sender. - it seems like the sender has been permanently blocked no matter what I do. Any specific settings I am missing or misunderstanding? Many tnx in advance for any help! best regards -- //Micke Is this with a Hotmail account? Check via web mail for your Junk mail folder and filtering...

Sorting some information help
HI, I have a sheet with 25 columns and up to 10000 rows. The number of rows will always be the same for each worksheet. and contain text information only. What I would like is a macro to show in 2 rows below the last row for each column, how many times the text that appears in row 1 occurs in row 2 thru (the last row) above it. Thanks in advance Chuck C Enter the following formula in cell A10002, and copy across to column Y: =COUNTIF(A2:A10000,A1) Chuck wrote: > I have a sheet with 25 columns and up to 10000 rows. The number of rows > will always be the same for each work...