|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Selection of all Text for a specific page in word is spanning selection across pages
I am trying to iterate through each and every page in RTF document
and select all the text page by page and apply certain business rules. Below mentioned is the code that i am using to select all the text page by page in RTF. ActiveDocument.Bookmarks("\Page").Range.Select() But i observe that it is selecting text from multiple pages. i.e. text from current page along with (text from previous page or from next page) Why is that the above code snippet not selecting text only from the current page but also includes text from next page as well ? Any suggestions to resolve this and ensure text from only the current page is selected. |
#2
|
||||
|
||||
Since page content in Word is dependent on the attached printer driver, unless your RTF document uses manual page breaks or Next Section breaks to delineate page boundaries, there is no reason to suppose the RTF pages will be the same as what you'll see in Word. That said, you don't need to select a page to work on it. It's far more effieicient to use range objects, for example:
Code:
Sub Demo() Dim Rng As Range, i As Long With ActiveDocument For i = 1 To .ComputeStatistics(wdStatisticPages) Set Rng = .GoTo(What:=wdGoToPage, Name:=i) Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page") With Rng 'Do something with the page range End With Next End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
This doesn't seem to be working.
Inspite of implementing the code suggested by you, i still find the text selection is spanning pages in RTF document. Attached is the screenshot where i am currently in page 3, but it selects text even from page 2 as well. How do i get rid of this kind of selection and ensure only text in the current page is selected ? |
#4
|
||||
|
||||
It appears you're working with a table, with cells spanning page boundaries and that merged/split cells are involved. Correct? If so, defining a range that spans all the columns on a given page may be causing the effect.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
So, How do i resolve this issue ?
Any code snippets would be of great help. |
#6
|
||||
|
||||
Dealing with this would probably require a fair bit of vba coding. If you can attach a document to a post with some representative data (delete anything sensitive), I'll take a look at it (no promises, though). You can attach files via the paperclip symbol on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Attached is the Zip file that has sample RTF document with which, i was trying to select text from each page , but i find text is being selected from current page as well as previous page or next page during the iteration.
Select text from page 2 and 3, you will find text being selected from multiple pages rather than that specific page. you can use this sample rtf document for your analysis. Thanks for your help in Advance. |
#8
|
||||
|
||||
OK. I've had a quick look at the document and can see what the problem is - cells spanning page boundaries. What is the purpose of the page-by-page processing? Can your needs be met by telling Word to not allow the cells to split across pages?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
My requirement is to generate a report which could range upto 1000 pages
and will have lots of tables in it. It's a business need that tables having too many rows and which cannot be accommodated in a single page will have to flow to next page and cannot be broken. So, preventing the cells spanning page boundaries is not desirable. Having said that, some times, i find an empty table row on a page in RTF file which (as you mentioned) would have got crossed the page boundaries. My main problem over here,i need to ensure that there are no empty pages or pages with empty table rows in the report. If there are any, then i need to get rid of them by removing these kind of blank pages. For the same reason, i am iterating through the whole RTF document page by page searching for blank pages and removing them. I need to achieve this programmatically. This is where the problem lies, where in it is not able to identify pages with empty table rows or cells, becoz a part of the text gets selected from previous page resulting the current page not to be considered as a blank page. But, actually, it's a blank page. Any help in resolving this issue would be great. |
#10
|
||||
|
||||
What you could try is cleaning up the table contents. I note that many cells have an empty first paragraph, plus text padding at the end. The following macro cleans up both, thus reducing each table's overall size.
Code:
Sub CleanUp() Dim Tbl As Table, TblCl As Cell, Rng As Range, RngDel As Range With ActiveDocument For Each Tbl In .Tables For Each TblCl In Tbl.Range.Cells Set Rng = TblCl.Range Rng.End = Rng.End - 1 Set RngDel = Rng.Characters.Last RngDel.Collapse wdCollapseEnd Do While Asc(RngDel.Characters.First.Previous) < 33 If RngDel.Start = Rng.Start Then Exit Do RngDel.Start = RngDel.Start - 1 Loop RngDel.Text = vbNullString If Rng.Paragraphs.Count > 1 Then If Len(Rng.Paragraphs.First.Range) = 1 Then Rng.Paragraphs.First.Range = vbNullString End If Next Next End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Printing a Selection in Word and keeping it in the same place on the page | punkrae | Word | 0 | 03-29-2012 10:49 AM |
Text Box Selection | GugaBFigueiredo | Word | 1 | 01-16-2012 09:04 PM |
Macro to populate a text form field based on dropdown selection | koloa | Word | 0 | 10-20-2011 11:52 AM |
table spanning multiple pages 'upside down' | lj_eco | Word Tables | 13 | 07-17-2011 04:39 PM |
Form Field - Drop down selection causing auto text | chesspupil | Word VBA | 7 | 05-09-2010 05:43 AM |