Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove blank line at the top of word doc.

Status
Not open for further replies.

neemi

Programmer
May 14, 2002
519
GB
How to I remove any blank lines at the top of a word doc through code.

The number of blank lines could vary so need to ensure only the blank lines are removed.

Help appreciated.
Cheers,
Neemi
 
In Case anyone needs to do the same as I needed here is the code I used in the macro:

Code:
Do Until Selection.EndKey(Unit:=wdLine, Extend:=wdMove) > 0
     Selection.Delete Unit:=wdCharacter, Count:=1
Loop

Hope it helps someone.
 
I hope that is not the only code you have!!!! You need to be very careful about where the Selection point is when you run that. If you run it anywhere that there is text BEFORE the Selection, then it is an infinite loop and the code will never stop.

Yes it works fine if there is ONLY blank lines before the Selection. But if there is not.... Say you have:

blank line
blank line
blank line
Text text text 1111
blank line
blank line
blank line
blank line
Text 22222
blank line
blank line
blank line

If you run your code at the end, it will remove "blank lines" up to "Text 22222" and then merrily continue forever in the loop.

One way to make this not happen would be to put Selection.HomeKey Unit:=wdStory to make sure the Selection goes to the beginning of the document.




Gerry
 

You shouldn't have so many documents with blank lines at the beginning that you need a macro for this rather trivial exercise. Yes, I know there are several ways for it to happen (click and type, for example) but happening, on a regular basis, when you don't want it suggests an automatic process may be creating them, in which case you should probably try to address that process instead of trying to provide a post hoc fix.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
I didn't even want to address the WHY of this. I completely agree. I wonder why there would even be "blank lines". A well put together document would not have any in the first place.

Gerry
 
I have a document that is created from access, using an almost find and replace method to complete the relevant spaces. Yes I know that a mailmerge could have been used but without going into it there were too many issues with it.

The top line of the document tempplate has a few characters which the find and replace code needs, so that it knows what it is looking for and this is enclosd in < and >. Once the macro has run and done what it does the top line is replaced with "", hence leaving a blank line or two.

Also the selection will always be at the top as I have set it to be there.

If you have any other questions or comments please feel free to post.

Cheers.
Neemi
 
OK, seems reasonable to me. My post was in response to seeing the code in isolation. That is why I asked if there was other code...which...there is. Someone using the code you posted, by itself, may have had problems. And...yes, I can now see why there would be blank paragraphs (I hate the use of the term "line"), and why you would want to get rid of them.

Thanks for replying.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top