Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Cursor to first/last line in doc in macro?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Karen.Reedy - 22 Feb 2007 14:16 GMT
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)
Rob - 22 Feb 2007 14:27 GMT
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
Karen.Reedy - 22 Feb 2007 14:30 GMT
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?

> Can be done several ways if this is what you mean.
>
[quoted text clipped - 6 lines]
> 'go to the last line
> Selection.GoTo What:=wdGoToLine, Which:=wdGoToLast
Rob - 22 Feb 2007 15:11 GMT
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.
Jean-Guy Marcil - 22 Feb 2007 15:51 GMT
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
[quoted text clipped - 6 lines]
> 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
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.