Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.tables
Robin Robin is offline
external usenet poster
 
Posts: 112
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   Report Post  
Posted to microsoft.public.word.tables
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
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   Report Post  
Posted to microsoft.public.word.tables
Robin Robin is offline
external usenet poster
 
Posts: 112
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   Report Post  
Posted to microsoft.public.word.tables
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
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   Report Post  
Posted to microsoft.public.word.tables
Robert M. Franz (RMF) Robert M. Franz (RMF) is offline
external usenet poster
 
Posts: 1,741
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   Report Post  
Posted to microsoft.public.word.tables
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
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   Report Post  
Posted to microsoft.public.word.tables
Robert M. Franz (RMF) Robert M. Franz (RMF) is offline
external usenet poster
 
Posts: 1,741
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
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
error messages when using table formula after moving column tot's hunguponword Tables 3 November 26th 06 03:27 AM
I have a table that spans multiple pages. JudyDeak Tables 2 July 24th 06 03:39 PM
Table breaks accross two pages - even though it is instructed not bvuk Microsoft Word Help 3 February 21st 06 07:50 PM
reformat table onto multiple pages Harry Tables 1 February 8th 06 05:11 PM
How do I set up a table title so that across pages it adds "cont'd Hopeful Tables 6 November 5th 05 01:55 AM


All times are GMT +1. The time now is 12:30 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"