Solved

Help with Loops

Posted on 2013-01-21
3
133 Views
Last Modified: 2013-12-24
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
Comment
Question by:jbarcher13
  • 2
3 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38802708
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
 

Accepted Solution

by:
jbarcher13 earned 0 total points
ID: 38807801
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
 

Author Closing Comment

by:jbarcher13
ID: 39737629
Because it works!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Outlook Free & Paid Tools
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now