Selections Converted to Ranges

J

jjfeenix

HI:

I use the following code that use ad variable (WhichTable) to locate a named
bookmark, which is inside a table. Next, I set variables I need to retrieve
the info from certain cells.

ActiveDocument.Bookmarks(WhichTable).Select
Selection.Collapse (wdCollapseStart)

Selection.Tables(1).Select
Set myTable = Selection.Tables(1)
Selection.Collapse (wdCollapseStart)

Set myCatCell = myTable.Range.Cells(1)
Set mySesCell = myTable.Range.Cells(3)
Set myGoalCell = myTable.Range.Cells(5)
Set sesGoalCell = myTable.Range.Cells(6)

Am I doing this efficiently? I'm wondering if I should be using Range
objects rather than Selections, to cut down on screen redrawing. If I am
correct, any help with converting selections to ranges would be greatly
appreciated.

TIA,
John
 
D

Doug Robbins - Word MVP

Better to use:

Dim myTable As Table
Dim myCatCell As Cell
Dim mySesCell As Cell
Dim myGoalCell As Cell
Dim sesGoalCell As Cell
Set myTable = ActiveDocument.Bookmarks(WhichTable).Range.Tables(1)
With myTable.Range
Set myCatCell = .Cells(1)
Set mySesCell = .Cells(3)
Set myGoalCell = .Cells(5)
Set sesGoalCell = .Cells(6)
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

George Lee

In general, ovoid Selects as much as you can. They cause a number of
problems. First, it makes the code document specific, meaning, that actions
will be on the active document only. That would be fine but in many
instances, you may have several documents open at once and you’ll want a way
to tell the documents apart. Second, during longer operations, users may be
checking their email or looking at other stuff, so you can’t always count on
the document you want as being the active document. Third, all actions are
directed to the active document. For example, you have selection made in the
active document and the user accidently hits the return or another key. The
entire selection gets replaced and you’ve lost control. Finally, there is the
speed factor. Select is a very slow call.

You should use ActiveDocument only once, at the moment you set the document
to its own variable. For example:

Dim tableDocument as Document
Set tableDocument = ActiveDocument
‘Even better: Set tableDocument = Application.Documents(“MyDocument.docâ€)

Now by referencing tableDocument, you’re absolutely certain of using that
one specific document. Similarly with ranges instead of Select:

Dim myRange as Range
Set myRange = tableDocument.tables(1).Cell(1).Range

Has the exact same effect as using Select, only much safer.
 
J

jjfeenix

Doug and George:

Thank you very much for the great lesson. I'm off to implement the info.

John
 

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

Top