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

Remove data from word document after position x

Status
Not open for further replies.

Greyfleck

IS-IT--Management
Jun 2, 2008
61
GB
I have a word document that contains a brochure. I need to remove all data after a given point as it is not needed. I guess that I need a simple macro that loops through the document, positions the cursor at point x (below 22 on the ruler) and then deletes all the rest of the data on that line.
Can anyone point me in the right direction please...

Many Thanks
Jon
 
Not sure that I can help, but can you be clearer about what you are trying to do? Is the brochure a form that gets sent back to you and then you only want to keep the top 22 (?lines). What are you keeping and what are you deleting?
 
Hi DrSimon. The brochure is just that - a listing of parts where each row consists on 90 characters of which all after the 50th character is nonsense as far as my requirements are concerned.

My task was to simply remove the 'rubbish' to make the data more manageable.

After posting I carried on researching possible options and came up with the following macro:

Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

As long as I position the cursor in the required place (I assigned this macro to alt+q) I could run the macro to delete each line ... the trick now would be to enable the macro to loop through my document (250 pages) so as I wouldn't have to manually place the cursor ...

Thank you for looking at my problem ...

Jon
 
You might want to look at using a bookmark either to position your cursor at the right place and then delete to end or to mark all the text that need to be deleted. You can use VBA to do the deletion once you have set the bookmark
Simon
 
Bookmark all that beforehand? Yikes. I don't think so. You may as well manually place the cursor and do the delete.

IF each "line" is actally a paragraph, then:
Code:
Sub After50()
Dim oPara As Paragraph
Dim r As Range

For Each oPara In ActiveDocument.Paragraphs
   If Len(oPara.Range.Text) > 50 Then
      Set r = ActiveDocument.Range( _
         Start:=oPara.Range.Start + 50, _
         End:=oPara.Range.End - 1)
      r.Delete
   End If
Next
End Sub
may work. You may have to adjust the numbers slightly, as it could keep a space after the 50th character. Note "character" includes spaces!


The code takes every paragraph greater than 50 characters, and deletes everything from the 50th character to the paragraph mark.

55,687.00 hours down....
<60 hours to go

tick tick tick tick tick
 
Thanks Fumei - I will give it a go and see what happens... [2thumbsup]
 
Hi Greyfleck,

if
- you use a non-proportional font (e.g. Courier New)
- and in each line (not paragraph) the characters from #50 to #90 are to be deleted
then
there is also a way to do this manually
- put the cursor after character #50
- press [Strg]+[Shift]+[F8] (at least in German Word, I don't know if this works in English Words)
- now you can move the cursor down and to the right. You will get a "column" selection although you are not in a table. When you hit [Delete] everything under this selection will be gone as if you had made a selection on a grid.*

HTH.

Markus
_______________
* This works in my Word2003. It is a very old feature of Word, already existing since DOS times. I could never think how it would come to its existence. But we have it, so why not use it.
 
AFAIK that should work in all versions. I would be a bit careful with it though. Use it in manageable chunks, say two or three pages at most, and check it! But yes, you can do that manually.

55,687.00 hours down....
<60 hours to go

tick tick tick tick tick
 
Thanks to all that replied - I will take all of this onboard and try out to see what works best for me.

Thanks again

Jon
 
Whoops! If that had been an exam I would have failed miserably as I didn't read the question (or in this case the answer) properly! Glad the others read it properly!
 
It happens to all of us.

55,687.00 hours down....
<60 hours to go

tick tick tick tick tick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top