Getting to the end of the page?

W

Webtechie

Hello,

I've been programming with VBA for a number of years now, only with Excel.
I am now creating a report with Word. I am trying to do something as basic as

1) Open a document
2) Go to the end of the document
3) Insert of new page

It is appears from the macro recording that I need to use

Selection.endkey unit:=wdstory

Question:

What is selection? I don't see any books on programming in Word. In Excel,
selection is cells that you have selected.

Why am I using selection.endkey, when I haven't selected anything?

Is this the right way to program an application to go to the end of the
document or to find out where I am at in the document?

Thanks,
 
J

Jay Freedman

The Selection object represents whatever text or other object is 'selected'.
It could be a collapsed point between two characters; a single character;
several to many characters; a table, graphic, or embedded object; or the
entire document. If your macro code moves the Selection, then the selected
stuff on the screen also changes, including scrolling the document if
necessary.

If you have experience in VBA, you know that objects can be manipulated in
code without changing the selection. This is true in Word, too -- you
declare a Range object (which has most of the same properties and methods as
the Selection object) and work with that. Unfortunately, the macro recorder
knows nothing about Range objects and always records the Selection, so
recorded macros should be edited to make that replacement.

Instead of Selection.EndKey, I'd recommend the sequence

Dim myRg as Range
Set myRg = ActiveDocument.Range
myRg.Collapse wdCollapseEnd

and then insert new material into myRg.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
W

Webtechie

Jay,

That is excellent advice. Thank you. I know nothing about the range syntax
using Word VBA. I do, however, have a lot of experience manipulating Excel
Spreadsheets and cells without actually selecting them.

One question:

Again, I'm not familiar with the range object. Now that I have a new page,
I need to open another word document which has a template (paragraph and
textboxes with charts in the textboxes).

Using the range ("myrg "), how would I open the document (in code), copy
everything and then bring in back to this new page of my active document?

Do you know of some examples for me?

Thanks again. I just might finish this project today! HooooRaw!!
 
J

Jay Freedman

Again, manipulating objects is easier, faster, and less error-prone than
pretending that the macro is a ghost user pressing buttons. In this case,
the InsertFile function pulls in the content of the file you specify,
without having to open that file or copy/paste anything.

Sub x()
Dim myRg As Range
Dim fn As String

fn = "c:\somefolder\somefile.doc"

Set myRg = ActiveDocument.Range
myRg.Collapse wdCollapseEnd

myRg.InsertFile FileName:=fn
End Sub

Browse through the articles listed at
http://word.mvps.org/FAQs/MacrosVBA.htm for more stuff like this.
 
W

Webtechie

Thanks Jay!!

Jay Freedman said:
Again, manipulating objects is easier, faster, and less error-prone than
pretending that the macro is a ghost user pressing buttons. In this case,
the InsertFile function pulls in the content of the file you specify,
without having to open that file or copy/paste anything.

Sub x()
Dim myRg As Range
Dim fn As String

fn = "c:\somefolder\somefile.doc"

Set myRg = ActiveDocument.Range
myRg.Collapse wdCollapseEnd

myRg.InsertFile FileName:=fn
End Sub

Browse through the articles listed at
http://word.mvps.org/FAQs/MacrosVBA.htm for more stuff like this.
 

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