Cursor to first/last line in doc in macro?

K

Karen.Reedy

My boss has decided that we're to fill out weekly project reports in Word.
He wants to be able to keep an archive in a separate document and keep the
template in the main document. I have created a macro to select the table
that contains the template, copy it, open the archive document, paste and
save. However, is there a way to ensure that the cursor either goes to the
very first or very last line in the document during the macro so that the
pasting stays in chrono order? (I tested mine and as long as no one messes
with it, i.e. opens the document, puts the cursor in the middle and then
saves, it works fine, but I'd like to idiot proof it)
 
R

Rob

Can be done several ways if this is what you mean.

'go to the beginning
Selection.HomeKey Unit:=wdStory
'go to the end
Selection.EndKey Unit:=wdStory
'got the first line
Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst
'go to the last line
Selection.GoTo What:=wdGoToLine, Which:=wdGoToLast
 
K

Karen.Reedy

Ok, so if I'm editing the VB code behind the macro (at this point I'm cursing
myself for not paying attention in that VB class I took in college!), this
would go at the very beginning so that it sets the insertion point properly,
yes?
 
R

Rob

Those were just examples, not something to copy and paste. Which you use and
where you put it depends on what you want to do. It wasn't clear if you
wanted to go to the first or last and when you wanted this to happen. If you
open a document and paste something, but want to go to the last line before
you paste it, then you might put the code between the open and paste actions
in the macro. Probably Selection.EndKey Unit:=wdStory is best because it will
go to the end even with multiple pages.
 
J

Jean-Guy Marcil

Karen.Reedy was telling us:
Karen.Reedy nous racontait que :
My boss has decided that we're to fill out weekly project reports in
Word. He wants to be able to keep an archive in a separate document
and keep the template in the main document. I have created a macro
to select the table that contains the template, copy it, open the
archive document, paste and save. However, is there a way to ensure
that the cursor either goes to the very first or very last line in
the document during the macro so that the pasting stays in chrono
order? (I tested mine and as long as no one messes with it, i.e.
opens the document, puts the cursor in the middle and then saves, it
works fine, but I'd like to idiot proof it)

It is better to work with he Range object then messing about with moving the
selection.

Something like this will paste whatever is on the clipboard at the end of
the active document:

'_______________________________________
Dim rgeDoc As Range

Set rgeDoc = ActiveDocument.Range

With rgeDoc
.Collapse wdCollapseEnd
.Paste
End With
'_______________________________________

But, to fully automate the procedure, and assuming that the procedure is
launched from the document that contains the table to archive, try something
like this:

'_______________________________________
Sub NoELtr()

Dim docTarget As Document
Dim rgeDocTarget As Range
Dim rgeSource As Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "Please, position the cursor in the table to archive before
proceeding."
Exit Sub
End If

Set rgeSource = Selection.Range.Tables(1).Range
Set docTarget = Documents.Open("C:\MyPath\MyWeeklyReportDocument.doc")

Set rgeDocTarget = docTarget.Range

With rgeDocTarget
.Collapse wdCollapseEnd
.FormattedText = rgeSource.FormattedText
End With

docTarget.Close wdSaveChanges

End Sub
'_______________________________________

--

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

Top