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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Empty Spaces

Status
Not open for further replies.

haedyr

Programmer
May 25, 2005
18
US
How would I go about deleting the empty space at the top of each page in a document? I'm guessing I can tell it move the cursor until it finds a character, during which time selecting that range. I can then selection.delete...but I'm missing something.

Too embarassed to show you my code....

Haedyr
 
That really depends on what the 'empty' space is.

If you mean a number of carrirage returns, it would simply be a matter of looping through story ranges in the document deleting all until the first non-empty story range is found.

Similar approach for empty pages created via page breaks.

If it's a paragraph formatting issue, simply go to the start of the document and format the first paragraph with a 0 leading space.


Cheers
 
I don't understand quite what you mean.

This is the code I created. Basically finding a certain text, trying to get the cursor to start at the begining of the text line and then back spacing 3 lines to start at the top of the page.

Selection.HomeKey (wdStory)

Do Until ActiveDocument.Bookmarks("\Sel") = ActiveDocument.Bookmarks("\EndOfDoc")

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Date Generated:"
.MatchWildcards = False
.Replacement.Text = ""
.Wrap = wdFindStop
.Forward = True
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeBackspace
Else
Exit Do
End If
Loop

It gets stuck at the backspace part, and won't run through the loop. It will move the first instance of the text, but won't continue. I took a class for this, but am realizing it could take years to figure all this out.

Also, how can I select a line that starts with a certain text and move it? I wrote a separate code for it and when it ran, only the cursor moved...my text did not.

Any help is MUCH appreciated!!
 
Hi Haedyr,

From your original post, I gained the impression that the number of 'empty spaces' to be deleted might have been variable. That would have required code to loop through the ranges on each page from the top down, deleting everything until the first non-empty space was encountered. So, on top of the code to do the looping, some definition of what constistuted 'empty spaces' would have been needed.

If all you need to do is to delete the found text and a set number of preceding characters, as now seems to be the case, then a search including wildcards should do:

Sub Test()
Dim MyRange As Range, i As Integer
With ActiveDocument
For i = 1 To .BuiltInDocumentProperties(wdPropertyPages)
Set MyRange = .GoTo(What:=wdGoToPage, Count:=i)
MyRange.Select
Selection.GoTo What:=wdGoToBookmark, Name:="\page"
With Selection.Find
.ClearFormatting
.Execute FindText:="????Date Generated:", MatchWildcards:=True, Forward:=True, ReplaceWith:=""
End With
Next i
End With
End Sub

In the above, each '?' is a wildcard representing one of the charaters preceding the found text to be inlcuded in the deletion.

Cheers
PS: you say you want to backspace 3 lines, but your code includes 5 backspaces - I compromised with 4!!
 
Hi Haedyr,

I've been thinking more about what you're trying to accomplish. Unless you have a particular need to process each page individually, the following code would be much more efficient:

Sub Test1()
With ActiveDocument
.Select
With Selection.Find
.ClearFormatting
.Execute FindText:="????Date Generated:", MatchWildcards:=True, Forward:=True, ReplaceWith:=""
End With
End With
Selection.HomeKey (wdStory)
End Sub

Cheers
 
Hi haedyr,

Basically finding a certain text, trying to get the cursor to start at the begining of the text line and then back spacing 3 lines to start at the top of the page.

Would you explain a little more about the situation? And please, never be embarrassed about posting code. Other than a friendly comment about possible ways you may improve things, no one is going to bite your head off. Or if they do, not to worry - the rest of us will bite back!

In any case, I am curious as to what exactly is going on. You are finding a "certain text", get the cursor to the "start of the line", then backspace to "the top of the page".

1. this assumes that ALL instances of the certain text are precisely three lines from the "top"of the page.

2. the top of the page is a dynamic location in Word - it can move. This makes for scary things when you hard code three backspaces.

3. using backspace is a very inefficient use of code. You can expand a selection easily to encompass four characters. rather than three commands to backspace.

EXAMPLE.

Code:
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeBackspace

is the equivalent to:

Code:
Selection.MoveStart unit:=wdCharacter, Count:=-4
Selection.Delete

or better yet:
Code:
With Selection
   .MoveStart unit:=wdCharacter, Count:=-4
   .Delete
End With

4.. It is better to use a Range object than a Selection.

macropod's code, as usual, does do the trick for the question stated, but I am wondering about the situation that creates such circumstances. WHY is the text where it is, and WHY are there the spaces?

Gerry
See my Paintings and Sculpture
 
Hi Gerry - I'm trying to delete all the empty lines leading up to the text "Date Generated". Since I couldn't figure out how to do that with code, I decided to take the easiest approach I could think of which is set the cursor at the beginning of the text, and hit backspace a few times. I don't want to delete the text, just move it to the top of the page it's on. (in most cases, the text "Date Generated" starts 7 lines down from the top of the page. The text "Date Generated" doesn't occur on all pages, which is why I'm saying for every instance of...delete the empty lines above it.


Additionally...how can I cut and paste a line? I want to cut a line that starts with a certain text, and move it to the 9th line down from the line "Date Generated" is on.
 
1, I abhor "empty" spaces. I never have "empty" spaces because I use Styles. My paragraph styles have the space between paragraphs built in. Which is one of the main purposes of styles. I strongly recommend you learn how to use them.

2. You state "in most cases" there are 7 lines. Again, if there are no "empty spaces", by using styles, this [roblem is solved. Hard coding the number of "empty spaces" is a problem, because what are you going to do if it is 5, or 6.

3. macropod's code, does do the trick, because it essentially creates a range from the start of the text to the pre-defined bookmark "\page".

4. Could you clarify if the page break, prior to "Date Generated" is hard coded (that is, was it created by a Ctrl-Enter, or a Insert > Break > Page Break? Or is it on a different page BECAUSE of the insertion of the "empty spaces" Clarification of this point will make it much easier to create code that will solve this issue.

5. RE: cut and paste a line. Do you mean a real line (the text that runs across ONE line), or do you mean one paragraph? By that I mean, what if the paragraph the "line" is part of extends to TWO lines? Would you want the whole paragraph moved? I am assuming so, but maybe the "line" will always be a short paragraph, and never extend to more than one line. Do you the ability to search for the line, or simply move a line that the cursor is in?

If you clarify these points, a solution can be done fairly easily.

Bottom line? These are design issues - as usual. Please post and I will respond ASAP.

Gerry
See my Paintings and Sculpture
 
It sounds as though this is some sort of report from a computer program that you are trying to customize, but there are too many details missing to really see what is going on.

You really should tell everyone more about what is going on. Particularly what causes the pagination the way the document currently exists.

I hope you realize that deleting lines will probably move the start of other pages in the document - you might need a more sophisticated approach to get the effect you want.
 
And by chance this generated report isn't a word document but a text file ...
 
Yes - this is a report generated out of a database program. It is normally copied and pasted into word...but I've just finally got my hands on it and have, for the time being, saved it as a .txt file. Since Excel is easier for me to program in (I don't know nearly as much about Word as I do about Excel), I was going to try to import the file into Excel and then do my coding. This, of course, isn't high priority for me as I have loads of other stuff at work going on.

To answer another question...cutting a line is one line not in a paragraph all by itself.

I'm going to try the Excel route because I just don't understand enough about word to understand clearly all of your suggestions to me.

I sincerely appreciate all of your help!

Haedyr
 
And why not simply playing with the FileSystemObject ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi Gerry,

If, as is now looking to be the case, the number of 'empty spaces' at the top of each page varies, then my code might not be appropriate as posted. The reason is that both versions assume a fixed number of spaces.

Plus, although the first version does indeed use the "\page" bookmark, the pagination is liable to change immediately the first match is encountered unless each page is preceded by a page break. To deal with that issue correctly, the line:
For i = 1 To .BuiltInDocumentProperties(wdPropertyPages)
should be changed to:
For i = .BuiltInDocumentProperties(wdPropertyPages) To 1 Step -1
That still leaves the question of the variable number of 'empty spaces' to deal with.

Cheers
 
I was wondering...is there the Trim() function in Word? Is that even a worthy option?

And, yes the white blank lines vary for each page...but I need to rethink this because I could solve this problem very easily by creating a new document at each instance of a manual page break, then I wouldn't care about the empty spaces at the top. I've found some code to play around with to create new documents from one doc....I'll probably have more questions. :)
I'm totally scatterbrained right now when it comes to this project. The first thing I learned was to write a project outline...which I haven't really done. Must rethink!
Thanks again everyone.

Haedyr
 
Hi Haedyr,

yes, there is a vba trim function in Word. AFAIK it won't remove paragraph breaks, though. In any event, you'd still have to find where the 'empty spaces' start and end.

Until you can provide us with more information about your document's structure, we're all winking in the dark ...

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top