Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selecting the whole page 1

Status
Not open for further replies.

lemonhalls

Technical User
Jun 11, 2004
52
CA
I have this code that is selecting only one page at a time.
I would like to apply this to the entire document, can someone please advice how would I change the code?

thanks
_______________________

Sub margin()

Dim myRange As Range

'moves the selection to the end
Selection.GoTo what:=wdGoToPage, Which:=wdGoToLast
'sets myRange to begin at first character in document and ends at the
'end of the first page
Set myRange = ActiveDocument.Range(Start:=0, End:=Selection.Range.End)

With myRange.ParagraphFormat
.LeftIndent = CentimetersToPoints(0.3)
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
With myRange.ParagraphFormat
.RightIndent = CentimetersToPoints(0)
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
ActiveWindow.ActivePane.VerticalPercentScrolled = 0
Selection.PageSetup.TopMargin = CentimetersToPoints(0.95)
ActiveWindow.ActivePane.VerticalPercentScrolled = 31
Selection.PageSetup.BottomMargin = CentimetersToPoints(1.27)

Selection.EndKey Unit:=wdStory, Extend:=wdMove

End Sub
 
Hi lemonhalls,

From what I can see, there is no need to make any selection, or range at all. The following seems to be what you are trying to do.

Code:
With ActiveDocument.Paragraphs
    .LeftIndent = CentimetersToPoints(0.3)
    .RightIndent = CentimetersToPoints(0)
End With

With ActiveDocument.PageSetup
    .TopMargin = CentimetersToPoints(0.95)
    .BottomMargin = CentimetersToPoints(1.27)
End With

Also, you have the following:

'moves the selection to the end
Selection.GoTo what:=wdGoToPage, Which:=wdGoToLast

'sets myRange to begin at first character in document and ends at the end of the first page

Set myRange = ActiveDocument.Range(Start:=0, End:=Selection.Range.End)

Are you sure it is only selecting one page? Selection, GoTo, Page, Last - goes to the last page of the document. So the range set should, in fact, be the entire document. But, again, I am not sure why you are making a selection, or a range, when it seems you are trying to change document wide attributes.

Am I missing something? Would not be the first time...



Gerry
 
Gerry,

I used your code, definitely more simple than mine and it works!

I am currently trying to import many documents into one active document.
My original thought was I had to apply the sub 'margin' to every page because the files all had a differnt layout.

I'm using the method insert to merge the files together.

I used your code and applied 'margin' to the first page and the settings seem to carry over to the next page! i assume it has to do with the insert next section page break that I'm using!?

Now I just call 'margin' once to the first page and the rest of the doc works fine!

Merci =)
 
The reason the settings carry over to the next page is the code does not apply to a page, or to a section. It applies to the ActiveDocument object, which means the whole document.

ActiveDocument.property means a property of the entire document.

All the Indents are properties of Paragraphs, which in turn is a property of the ActiveDocument object. All the Margins are properties of PageSetup, which is also a property of the ActiveDocument object.

So, you can make the code even shorter by using this:

Code:
With ActiveDocument
    .Paragraphs.LeftIndent = CentimetersToPoints(0.3)
    .Paragraphs.RightIndent = CentimetersToPoints(0)
    .PageSetup.TopMargin = CentimetersToPoints(0.95)
    .PageSetup.BottomMargin = CentimetersToPoints(1.27)
End With


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top