[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

Help with Loops

Greetings Experts! I need help with a Word 2010 macro. I need to create a macro that finds all the Heading 1 styles, moves 1 character to the right, then down one line, and then sets the Space Before property of that paragraph to "0". Sounds simple and for the most part, it is. However, I need help with a loop and have tried a number of things that haven't worked!

After trying a number of things (that haven't worked), I've written a macro that works for the first instance of Heading 1, but not the rest in the document.

Here's the macro:


Option Explicit

Sub Macro2()
'
' Macro2 Macro
'

ActiveDocument.Paragraphs(1).Range.Select
Selection.Collapse Direction:=wdCollapseStart

    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 1")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchByte = False
        .CorrectHangulEndings = True
        .HanjaPhoneticHangul = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchFuzzy = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=1
    With Selection.ParagraphFormat
       
        .SpaceBefore = 0
       
    End With
End Sub

I publish to Word out of a component content management system, which uses a Word template to transform the data. The document layout is 2-column. At the beginning of each section, there is a section break with a Heading 1 (which I use to populate the header). The following content also needs a section break. Because I need to use Before Spacing for my heading styles in the document, I need a way to make sure that the Space Before for the heading that falls at the next top of column is set to "0." I have written a macro that handles all subsequent column breaks in a section by setting the Layout Options in the AutoOpen macro (works great). However, the first heading that falls into Column 2 will not adhere to the layout options (Suppress Space Before Hard Page or Column Break) because of the section break prior to it, containing Heading 1.

I would love any and all help with this, please!

Regards, Jan
0
jbarcher13
Asked:
jbarcher13
  • 2
1 Solution
 
GrahamSkanCommented:
Try it like this:
Sub SetSpaceBefore()
    Dim rngFind As Range
    Dim rngPara As Range
    
    Set rngFind = ActiveDocument.Range
    With rngFind.Find
        .Style = "Heading 1"
        Do While .Execute
            Set rngPara = rngFind.Duplicate
            rngPara.Move wdParagraph, 1
            rngPara.ParagraphFormat.SpaceBefore = 0
        Loop
    End With
End Sub

Open in new window

0
 
jbarcher13Author Commented:
Hi Graham,

Well this isn't very elegant, but it works and I'm happy! Here's what I came up with:

Option Explicit
Dim rng As Object
'This macro changes the Space Before for paragraphs in Column 2, following Heading 1 in a section

Sub FixTopOfColumnParaSpace()
    Set rng = ActiveDocument.Content
    With rng.Find
        .Style = "Heading 1"
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
    End With
 
    While rng.Find.Execute
        rng.Select
        Selection.MoveRight Unit:=wdCharacter, Count:=2
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.ParagraphFormat.SpaceBefore = 0
    Wend
    
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
    
End Sub

Open in new window


If you have any other suggestions, that would be great!

Thank you so much! Jan
0
 
jbarcher13Author Commented:
Because it works!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now