PDA

View Full Version : Solved: Select text and then save selection



petedw
05-11-2005, 08:36 AM
Hi,

Could you please tell me if it is possible to use vba to select the 1st 5 pages of a document and save the selection as "x1" and then select the next 5 pages of the document and save that selection as "x2" and so on....

Thanks for your help

Pete

MOS MASTER
05-11-2005, 11:20 AM
Hi Pete, :D

Yes this is possible, but it would have been so much easier if your question would be to save each page to there seperate file!

It's not so here we go:
Sub Every5PageSave()
Dim oDoc As Word.Document
Dim oRange As Word.Range
Dim iCnt As Integer
Dim iPages As Integer
Dim iPage As Integer
Dim iEnd As Long
Dim iNr As Integer

Set oDoc = Application.ActiveDocument
Set oRange = oDoc.Range(0, 0)
iPages = oDoc.BuiltInDocumentProperties(14)
iPage = 5
iNr = 1

Application.ScreenUpdating = False
For iCnt = 1 To iPages Step 5
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=iPage, Name:=""
Selection.Bookmarks("\Page").Select

iEnd = Selection.End
Set oRange = oDoc.Range(oRange.End, iEnd)
oRange.Copy

Application.Documents.Add
Selection.PasteSpecial

With Application.ActiveDocument
.SaveAs FileName:=oDoc.Path & Application.PathSeparator & "x" & iNr & ".doc"
.Close
End With

oDoc.Activate

If iPages - iPage > 5 Then
iPage = iPage + 5
Else
iPage = iPages
End If

iNr = iNr + 1
Next

Selection.HomeKey Unit:=wdStory

Set oRange = Nothing
Set oDoc = Nothing
End Sub


I really hate my own coding in this one because I'm sure this could be programmed more eliquent! (Yes guys this is an invitation to trash it) :rofl:

On the other hand it works so...

Enjoy! :whistle:

fumei
05-11-2005, 12:34 PM
It would also help if you tell us what you want to do.

For example, you could RETAIN those selection, and be able to retrieve them whenever you want by making them bookmarks.

Bookmarks can be a single character, a paragraph, a page, or the entire document. You can also nest bookmarks within bookmarks.

So. You could select the text for page 1 to 5, then go Insert > Bookmarks, give the selection a name (say Page1_5), press OK. You are done. Do the same for pages 5 to 10 (named....Pages5_10).

Now when EVER you want to get that text it is available manually by simply Insert > Bookmark > Pages1_5. Voila! The text is selected.

By code:
Selection.GoTo What:=wdGoToBookmark, Name:=Pages1_5

Again, voila the text is selected.

fumei
05-11-2005, 12:36 PM
Oh...and these bookmarks are saved with the document. Once created they are there, in the document.

MOS MASTER
05-11-2005, 12:44 PM
Yeah..love bookmarks! :yes

I thought it was some kind of mailmerge document that would merge in pieces of 5 pages!

But I'm also curious to how the code will be applied..:whistle:

MOS MASTER
05-13-2005, 11:17 AM
Hi Pete, :D

Was my code helpfull? Or do you need something else? :whistle:

MOS MASTER
05-14-2005, 09:21 AM
Glad to see your problems solved now! :yes