PDA

View Full Version : Detecting existing text in a selection



PhilBert1963
06-30-2010, 07:54 AM
I'm a novice at VBA so try and not sound too ignorant. I have a situation where I receive a report in standard text. The report has no page breaks only paragraphs, one paragraph per line. I’m taking the report changing it from 8.5”x11” to 8.5”x5.5”, reducing the text size from 10.5 to 7.5 or 8 depending on the report. I can do all this just fine. The trouble comes in when I insert a page break. I count down 36 lines at this point I’m selecting 8 lines and I want to see if any text exists. I don’t care what the text is only if there is any. If there is then I need to search for the text string “Page#” and insert a page break. If there is no text then I need to delete 6 lines to bring the next page up to the top of the sheet. I have no idea how to do the section in red. Below is what I have so far. Any help would be greatly apreciated.

Sub GeoReport_XTB()
' GeoReport Macro
'
'
Application.EnableCancelKey = wdCancelDisabled
Selection.Delete Unit:=wdCharacter, Count:=3

With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(0.2)
.BottomMargin = InchesToPoints(0.5)
.LeftMargin = InchesToPoints(0.2)
.RightMargin = InchesToPoints(0.2)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0)
.FooterDistance = InchesToPoints(0)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(5.5)
.SectionStart = wdSectionNewPage
End With

Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Font.Name = "Courier New"
Selection.Font.Size = 8
Selection.HomeKey Unit:=wdStory
Selection.MoveUp Unit:=wdLine, Count:=1

Selection.MoveDown Unit:=wdLine, Count:=36
Selection.MoveDown Unit:=wdLine, Count:=8, Extend:=wdExtend
End Sub

fumei
06-30-2010, 10:47 AM
1. It is best to avoid using Selection.

2. I am having a hard time figuring out what you are having difficulties with.

If you are Selecting, you can see what is selected. The is the point of using Selection.

PhilBert1963
06-30-2010, 11:14 AM
As I said I'm new to this. If there is a better way I'm very willing to learn. I've attached two image files and the text file I'm using for testing. Hopefully they help explain a little better. I'm not sure if my image files made it. Please let me know if they didn't.

fumei
06-30-2010, 11:18 AM
No attachments.

PhilBert1963
06-30-2010, 11:29 AM
I'll try it again My files were to big. It looks like I'll need to do it in three replys.

PhilBert1963
06-30-2010, 11:30 AM
This

PhilBert1963
06-30-2010, 11:32 AM
This is 3 out of 3 attachments

fumei
06-30-2010, 11:48 AM
All the PNG are unreadable, that is, so fuzzy I can not see anything on them.

Your file is just a bunch of plain text and does not help to explain anything.

fumei
06-30-2010, 11:49 AM
Try writing your code you have problems with INTO the file you attach.

PhilBert1963
06-30-2010, 12:36 PM
Okay this time I made a zip file. I included two images of much better quality and the file after the VBA that I have so far has been run against the original file previous posted. As far as writing the code I've tried so many different things and have come up with nothing that I realy have no idea what to put in. I'm not looking for someone to write the rest of this for me just give me ideas that I can work from.

PhilBert1963
06-30-2010, 12:40 PM
Sorry, wrong docx file in the zip file. Attached is the correct one.

fumei
06-30-2010, 12:49 PM
So the text that has lines like:

373466.2680 1175431.0676 2073.4177 3.0159 INT/ SHLD
___________________________________________________________________________ _____________________

0+28.00R1[CG] 373473.2981 1175427.6844 2073.2992 -2.9745 INT/ SHLD


that is, it is NOT before a Page X, you want to keep?

PhilBert1963
06-30-2010, 12:52 PM
Yes thos lines need to be on a page all to them selfs. Then the line with PageX in it will start on a new page.

fumei
06-30-2010, 01:11 PM
Try this. Click "Page Break" on top toolbar. You are using - UGH! - 2007 so I do not know if this will show correctly for you. It is NOT a 2007 file. In case it does not, here is the code:
Sub PageB()
Dim oPara As Paragraph
Dim r As Range

For Each oPara In ActiveDocument.Paragraphs
If InStr(1, oPara.Range.Text, "Page#") > 0 Then
Set r = ActiveDocument.Range( _
Start:=oPara.Range.Start, _
End:=oPara.Range.Start)
r.InsertBreak Type:=wdPageBreak
End If
Next
For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Text = vbCr Then
oPara.Range.Delete
End If
Next
' delete first break if at start
Set oPara = ActiveDocument.Paragraphs(1)
If Left(oPara.Range.Text, 1) = Chr(12) Then
oPara.Range.Text = _
Right(oPara.Range.Text, Len(oPara.Range.Text) - 1)
End If
End Sub
What it does is look for paragraphs with "Page#", inserting a page break before that paragraph. It then looks for those "empty" paragraphs, and deletes them. It then check to see if the document starts with a page break (it does now), and removes THAT page break.

Your paragraphs of:

___________________________________________________________________________ _____________________

that are NOT before a Page# are retained.