Hard to say, as I really do not know what you mean by: "The reason I'm doing it with Selection is that it's creating a whole document including tables, text, lists and more and it's doing it all progressively."
Creating a whole document including tables, text, lists etc, does not require Selection.
Selection means one thing....where the visible cursor is (and what it is "selecting").
Nothing more. It is the
visible location on-screen.
Say the cursor (Selection) is, oh, let's make it easy, right at the start of the document. Let's make it even easier...it is a brand new, blank, document. So the cursor (Selection) is blinking away at the top of the document.
Now, let's say you have two other documents, SomeText.doc, and SomeOtherText.doc.
SomeText.doc
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
SomeOtherText.doc
The slender monster barracuda bites hard. The slender monster barracuda bites hard. The slender monster barracuda bites hard.
The slender monster barracuda bites hard. The slender monster barracuda bites hard. The slender monster barracuda bites hard.
Got it? Now look at this.
Code:
Option Explicit
Sub DoStuff()
Dim r As Range
Dim oTable As Table
Dim oCell As Cell
Dim oHF As HeaderFooter
Set r = ActiveDocument.Range
With r
.InsertFile FileName:="c:\zzz\SomeText.doc"
.Collapse 0
End With
Set oTable = _
ActiveDocument.Tables.Add(Range:=r, _
NumRows:=3, _
NumColumns:=3, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)
Set r = ActiveDocument.Range
With r
.Collapse 0
.InsertParagraphAfter
.InsertFile FileName:="c:\zzz\SomeOtherText.doc"
.Collapse 0
.InsertBreak Type:=wdSectionBreakNextPage
End With
For Each oCell In oTable.Range.Cells
oCell.Range.Text = "some yadda"
Next
Set oHF = ActiveDocument.Sections(2) _
.Headers(wdHeaderFooterPrimary)
With oHF
.LinkToPrevious = False
.Range.Text = "This is text for Section 2 Header"
End With
End Sub
The result?
[Section 1 no Header text]
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
[table here]
some yadda some yadda some yadda
some yadda some yadda some yadda
some yadda some yadda some yadda
Section break
[Section 2 header text = "This is text for Section 2 Header"
The slender monster barracuda bites hard. The slender monster barracuda bites hard. The slender monster barracuda bites hard.
The slender monster barracuda bites hard. The slender monster barracuda bites hard. The slender monster barracuda bites hard.
All without EVER moving, or using, Selection. The cursor (Selection) is still blinking at the top of the document. Of course, at the end, you COULD move the cursor (Selection) to where ever you want.
Just to walk you through....
1. make a range object of the document (empty for now)
2. insert the file SomeText.doc. The range object is resized.
3. collapse the range object to its end (.Collapse 0)
4. set a 3 x 3 table object
5. RESET the range object for the whole document (to include the table)
6. collapse the range object to its end
7. insert a paragraph
8. insert the file SomeOtherText.doc
9. collapse the range object to its end
10. make a Section break (Next Page)
11. put "some yadda" in every cell of the table - but this
could be anything. I did this to be simple. Note that because you are dealing with a table
object, and you are NOT using Selection, you can action the table even though you are now "in" another Section.
12. set a HeaderFooter object to be the header of Section 2.
13. make that header unlinked to the previous one (Section 1)
14. put the text "This is text for Section 2 Header" in the headerfooter object.
Note that Section 1 remains blank.
This has everything to do with what Skip mentioned (although greatly expanded).
Skip: "You can and ought to REFERENCE the cell that your program is working on"
It is the reference to
objects that is the key to really using VBA, be it Word or Excel, or any other VBA compliant application.
By using objects fully, you can avoid using Selection, or Activate. Why? Because both of those use the resources to change things on screen. When you execute the demo code above, the document just
becomes the result of the instructions.
Gerry