Selection.Bookmarks("\page").Select dupes tables

J

James

In the below code, I wish to iterate through the tables on the selected
pages. What I find is that if a table is split across two pages, the
code picks up the table twice. I assume the fault lies with
"Selection.Bookmarks("\page").Select". Does anyone know why this
happens and/or have a workaround?

Tks


For i = x To y
Selection.GoTo _
what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=i
Selection.Bookmarks("\page").Select
Set sourcePage = Selection.Range
NumOfTables = Selection.Tables.Count
For z = 1 To NumOfTables
Set numPage = sourcePage.Tables(z).Range
numOfPage = numPage.Information(wdActiveEndPageNumber)
DetermineTableInfo sourcePage.Tables(z), TableData,
NumOfRows, NumOfColumns, _
ColHeaderStart, ColHeaderEnd,
FirstDataRow, _
IsColumnFormatAsPercent, title
WriteToWorkBook wb, TableData, Outputoffset, NumOfRows,
NumOfColumns, ColHeaderStart, _
ColHeaderEnd, FirstDataRow,
IsColumnFormatAsPercent, title, numOfPage
Next
Next
 
D

Dave Lett

Hi James,

The simple answer to your question is that it happens because part of the
table in on the current page. The work around, seems to me at least, is to
use tables rather than cycling through a bunch of pages that may or may not
have a table (it's inefficient). Also, it's a little difficult to track/read
your code. For all the code that you write, you should dimension your
variables (Dim sourcePage as Range, for example) and name your variables so
that you don't need to know what you've dimensioned them as. From what I can
see, you could start rewriting with something like the following:

Dim oTbl As Table
Dim iTblPag As Integer
Dim oTblRng As Range
For Each oTbl In ActiveDocument.Tables
Set oTblRng = oTbl.Range
iTblPag = oTblRng.Information(wdwdActiveEndPageNumber)
DetermineTableInfo oTbl ...
WriteToWorkBook ...
Next oTbl

If you don't use the variable more than once, then there's not a good reason
to 1) dimension the variable nor 2) set the variable. See oTblRng in the
example above. I dimension it, set it, and use it only once. There's no
saving in number of lines of code or any significant reduction in the line
where it's used.

HTH,
Dave
 
J

James

Dave,

When you say "The work around, seems to me at least, is to
use tables rather than cycling through a bunch of pages " would you
mind describing what you have in mind. I'm a newbie to word VBA, so I
need pretty detailed instrux. Tks much for that and the coding
suggestions.
 
D

Dave Lett

Hi James,

If you look at the code I provided, you'll see that it cycles through the
tables collection of the ActiveDocument

Dim oTbl as Table
For Each oTbl in ActiveDocument.Tables
Debug.Print oTbl.Information(wdwdActiveEndPageNumber)
Next oTbl

The sample I wrote out in the last message is how I'd handle it.

HTH,
Dave
 
J

James

The trick is that I don't want all tables in the document, I just want
tables from pages specified by the user.
 
J

James

The trick is that I want to print out the tables only for pages
specified by the user, not the whole document. How would you approach
that?
 
J

James

The trick is that I want to print out the tables only for pages
specified by the user, not the whole document. How would you approach
that?
 
J

James

The trick is that I want to print out the tables only for pages
specified by the user, not the whole document. How would you approach
that?
 
J

James

The trick is that I just want to show the tables on the pages the user
has selected, not all tables. Any ideas?
 
J

James

The trick is that I only want to print out the pages selected by the
user, not the whole document. How would you approach this? (The user
types in the pages desired).
 
J

Jean-Guy Marcil

James was telling us:
James nous racontait que :
The trick is that I don't want all tables in the document, I just want
tables from pages specified by the user.

Why did you send this 6 times in one hour?

Then define a range that spans the pages you want.
Go to the page you want, pick up its Start property, go the last page, pick
up its End property, use that to set up a range.
Then iterate through the tables in that range, like so:

'_______________________________________
Dim MyStart As Long
Dim MyEnd As Long
Dim MyRange As Range
Dim MyTable As Table
'to save user starting selection:
Dim StartRange As Range

Set StartRange = Selection.Range

Selection.GoTo wdGoToPage, wdGoToAbsolute, 3
MyStart = Selection.Start
Selection.GoTo wdGoToPage, wdGoToAbsolute, 5
MyEnd = Selection.Bookmarks("\page").Range.End

Set MyRange = ActiveDocument.Range(MyStart, MyEnd)
MyRange.Select

With MyRange
For Each MyTable In .Tables
With MyTable
.Select
'do your stuff
End With
Next
End With

StartRange.Select
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Jean-Guy Marcil was telling us:
Jean-Guy Marcil nous racontait que :
James was telling us:
James nous racontait que :


Why did you send this 6 times in one hour?

Never mind.
You probably posted through the horrible death inducing new improved Google
Beta crap site. I can't believe they would come up with something so inept,
they are usually so good at what they do...

I am totally disappointed.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top