A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Tables
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Detect how many pages a table is



 
 
Thread Tools Display Modes
  #1  
Old January 3rd, 2007, 04:25 PM posted to microsoft.public.word.tables
Robin
external usenet poster
 
Posts: 481
Default Detect how many pages a table is

I am using Word 2003 SP 2. Is there a way to select a table and determine
how many pages it is and return that value at my insertion (cursor) point?
--
Thanks in advance!
  #2  
Old January 3rd, 2007, 05:34 PM posted to microsoft.public.word.tables
Jay Freedman
external usenet poster
 
Posts: 9,488
Default Detect how many pages a table is

Robin wrote:
I am using Word 2003 SP 2. Is there a way to select a table and
determine how many pages it is and return that value at my insertion
(cursor) point?


The following macro will display the number of pages in a message box.

Sub PagesInTable()
Dim startPg As Long, endPg As Long
Dim oRg As Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "Place the cursor in a table", , "Error"
Exit Sub
End If

ActiveDocument.Repaginate

Set oRg = Selection.Tables(1).Range
With oRg
endPg = .Information(wdActiveEndPageNumber)
.Collapse wdCollapseStart
startPg = .Information(wdActiveEndPageNumber)
End With

MsgBox "Table occupies " & endPg - startPg + 1 & " page(s)"

Set oRg = Nothing
End Sub

Inserting the number at the insertion point is problematic -- if some text
or the whole table is selected, it will be replaced by the number, which
probably isn't the effect you wanted. You could collapse the Selection to
its start, which would prevent that.

Also be aware that the macro works off of page numbers alone. For example,
if the table starts near the bottom of page 1 and ends near the top of page
2, the macro will say that it occupies 2 pages, even though it's less than
one page long.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


  #3  
Old January 3rd, 2007, 06:33 PM posted to microsoft.public.word.tables
Robin
external usenet poster
 
Posts: 481
Default Detect how many pages a table is

Thanks Jay, this works great! I figured out how to return the value at my
insertion point, but the problem is that if the table's page length changes,
the value doesn't change. Any advice?
--
Thanks in advance!


"Jay Freedman" wrote:

Robin wrote:
I am using Word 2003 SP 2. Is there a way to select a table and
determine how many pages it is and return that value at my insertion
(cursor) point?


The following macro will display the number of pages in a message box.

Sub PagesInTable()
Dim startPg As Long, endPg As Long
Dim oRg As Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "Place the cursor in a table", , "Error"
Exit Sub
End If

ActiveDocument.Repaginate

Set oRg = Selection.Tables(1).Range
With oRg
endPg = .Information(wdActiveEndPageNumber)
.Collapse wdCollapseStart
startPg = .Information(wdActiveEndPageNumber)
End With

MsgBox "Table occupies " & endPg - startPg + 1 & " page(s)"

Set oRg = Nothing
End Sub

Inserting the number at the insertion point is problematic -- if some text
or the whole table is selected, it will be replaced by the number, which
probably isn't the effect you wanted. You could collapse the Selection to
its start, which would prevent that.

Also be aware that the macro works off of page numbers alone. For example,
if the table starts near the bottom of page 1 and ends near the top of page
2, the macro will say that it occupies 2 pages, even though it's less than
one page long.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.



  #4  
Old January 4th, 2007, 04:29 AM posted to microsoft.public.word.tables
Jay Freedman
external usenet poster
 
Posts: 9,488
Default Detect how many pages a table is

I've seen that kind of behavior intermittently, but I can't reproduce
it at will. The ActiveDocument.Repaginate command is supposed to make
sure the page numbers are stable before the .Information function
runs, but it might not be completing fast enough.

You might be able to fix it by inserting a delay of a second or two
after the repaginate command, or maybe you can force repagination by
going to Print Preview and back.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Wed, 3 Jan 2007 10:33:03 -0800, Robin
wrote:

Thanks Jay, this works great! I figured out how to return the value at my
insertion point, but the problem is that if the table's page length changes,
the value doesn't change. Any advice?
--
Thanks in advance!


"Jay Freedman" wrote:

Robin wrote:
I am using Word 2003 SP 2. Is there a way to select a table and
determine how many pages it is and return that value at my insertion
(cursor) point?


The following macro will display the number of pages in a message box.

Sub PagesInTable()
Dim startPg As Long, endPg As Long
Dim oRg As Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "Place the cursor in a table", , "Error"
Exit Sub
End If

ActiveDocument.Repaginate

Set oRg = Selection.Tables(1).Range
With oRg
endPg = .Information(wdActiveEndPageNumber)
.Collapse wdCollapseStart
startPg = .Information(wdActiveEndPageNumber)
End With

MsgBox "Table occupies " & endPg - startPg + 1 & " page(s)"

Set oRg = Nothing
End Sub

Inserting the number at the insertion point is problematic -- if some text
or the whole table is selected, it will be replaced by the number, which
probably isn't the effect you wanted. You could collapse the Selection to
its start, which would prevent that.

Also be aware that the macro works off of page numbers alone. For example,
if the table starts near the bottom of page 1 and ends near the top of page
2, the macro will say that it occupies 2 pages, even though it's less than
one page long.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.



  #5  
Old January 6th, 2007, 06:15 PM posted to microsoft.public.word.tables
Robert M. Franz (RMF)
external usenet poster
 
Posts: 1,743
Default Detect how many pages a table is

Hi Jay

Jay Freedman wrote:
I've seen that kind of behavior intermittently, but I can't reproduce
it at will. [..]
Thanks Jay, this works great! I figured out how to return the value at my
insertion point, but the problem is that if the table's page length changes,
the value doesn't change. Any advice?


I guess the OP is talking about a much later changes to the table (or,
as it is, a pagination reflow that would change the calculated value as
well).

[I'm not sure there's a good answer to that request, except running the
macro again on each table prior to printing ...]

2cents
Robert
--
/"\ ASCII Ribbon Campaign | MS
\ / | MVP
X Against HTML | for
/ \ in e-mail & news | Word
  #6  
Old January 6th, 2007, 10:32 PM posted to microsoft.public.word.tables
Jay Freedman
external usenet poster
 
Posts: 9,488
Default Detect how many pages a table is

Hi Bob,

The behavior I saw didn't involve the value inserted in the document.
I was using the macro I originally posted, with just a MsgBox
displaying the number of pages. If I ran the macro on a table of, say,
2 pages; then added another page or two of new rows to the table; and
reran the macro, the MsgBox still showed 2 pages. Single-stepping
through the macro showed that the value returned by the line

endPg = .Information(wdActiveEndPageNumber)

didn't change, even after the Repaginate command. But when I ran the
same test on another document, I couldn't reproduce that behavior.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Sat, 06 Jan 2007 19:15:03 +0100, "Robert M. Franz (RMF)"
wrote:

Hi Jay

Jay Freedman wrote:
I've seen that kind of behavior intermittently, but I can't reproduce
it at will. [..]
Thanks Jay, this works great! I figured out how to return the value at my
insertion point, but the problem is that if the table's page length changes,
the value doesn't change. Any advice?


I guess the OP is talking about a much later changes to the table (or,
as it is, a pagination reflow that would change the calculated value as
well).

[I'm not sure there's a good answer to that request, except running the
macro again on each table prior to printing ...]

2cents
Robert
--
/"\ ASCII Ribbon Campaign | MS
\ / | MVP
X Against HTML | for
/ \ in e-mail & news | Word

  #7  
Old January 7th, 2007, 01:15 PM posted to microsoft.public.word.tables
Robert M. Franz (RMF)
external usenet poster
 
Posts: 1,743
Default Detect how many pages a table is

Hi Jay

Jay Freedman wrote:
[..]
didn't change, even after the Repaginate command. But when I ran the
same test on another document, I couldn't reproduce that behavior.


Ah well: don't we all hate these "non-repro" issues!

Greetings
Robert
--
/"\ ASCII Ribbon Campaign | MS
\ / | MVP
X Against HTML | for
/ \ in e-mail & news | Word
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 05:21 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.