PDA

View Full Version : Solved: VBA Section Difficulties



AndrewPerl
10-27-2005, 11:32 AM
I am trying to write a macro that will go through a document a reapply base page settings if an author has accidentally/on purpose altered them.

My dilemna is this...

1. What VBA command should I use to get the current section number?

2. How can I move the selection point to the beginning of each section to apply my page settings (they will be different for portrait and landscape pages)?

I have shelled out the macro as a For/Next Loop. Currently, the macro checks the PageSetup.Orientation property (0=portrait/1=landscape) and then uses the Select Case statement to apply various PageSetup properties.

Any advice on how to get items 1 and 2 working would be greatly appreciated. Any other comments on my approach would also be welcome.

Thanks,

Andrew in Kansas City

TonyJollans
10-27-2005, 12:17 PM
Hi Andrew,

Welcome to VBAX!

1. Selection.Range.Sections(1).Index - but you probably don't need to know it and shouldn't be working with the selection

2. (again) you shouldn't be working with the selection. Make your loop For Each Sect in ActiveDocument.Sections and then use, for example, Sect.PageSetup....

AndrewPerl
10-27-2005, 01:27 PM
Here is what I have so far (still not working)... any suggestions???

For Each Sect In ActiveDocument.Sections
If PageSetup.Orientation = wdPortrait Then
.PageSetup
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.TopMargin = InchesToPoints(0.8)
.BottomMargin = InchesToPoints(0.8)
.GutterPos = wdGutterPosLeft
.Gutter = 0
Else
.PageSetup.Orientation = wdLandscape
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.TopMargin = InchesToPoints(0.8)
.BottomMargin = InchesToPoints(0.8)
.GutterPos = wdGutterPosLeft
.Gutter = 0
End If
Next Sect

It has been almost ten years since I have tried any of this. I also used WordBasic instead of VBA. The double whammy of remembering how to code and learning the new VBA syntax is somewhat challenging.

Thanks for any help.

Andrew in KC

mdmackillop
10-27-2005, 01:32 PM
Hi Andrew,
If you select your code and click on the VBA button it formats it as shown, making it more readable.
Regards
MD

mdmackillop
10-27-2005, 01:40 PM
Hi Andrew,
I've not tested this but try

Sub setup()
For Each sect In ActiveDocument.Sections
If sect.PageSetup.Orientation = wdPortrait Then
With sect.PageSetup
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.TopMargin = InchesToPoints(0.8)
.BottomMargin = InchesToPoints(0.8)
.GutterPos = wdGutterPosLeft
.Gutter = 0
End With
Else
With sect.PageSetup
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.TopMargin = InchesToPoints(0.8)
.BottomMargin = InchesToPoints(0.8)
.GutterPos = wdGutterPosLeft
.Gutter = 0
End With
End If
Next sect
End Sub

AndrewPerl
10-27-2005, 02:45 PM
MD,

Thanks for the assist on this! The formatting for the landscape and portrait sections will be slightly different in the final version of this macro.

I am so glad that I found this site!

Thanks,

Andrew in KC

mdmackillop
10-27-2005, 02:49 PM
I am so glad that I found this site!

Don't forget to tell your friends!

Glad to help
MD