Consulting

Results 1 to 7 of 7

Thread: Solved: VBA Section Difficulties

  1. #1

    Solved: VBA Section Difficulties

    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

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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....
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3

    Reworked VBA to Reformat section (not working yet)

    Here is what I have so far (still not working)... any suggestions???

    [VBA] 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
    [/VBA]
    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

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Andrew,
    If you select your code and click on the VBA button it formats it as shown, making it more readable.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Andrew,
    I've not tested this but try
    [VBA]
    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


    [/VBA]
    Last edited by mdmackillop; 10-27-2005 at 01:50 PM. Reason: Missing "Sect" added. Trial margin changes corrected.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6

    It works!!!

    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

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by AndrewPerl
    I am so glad that I found this site!
    Don't forget to tell your friends!

    Glad to help
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •