?
Solved

Help with Loops

Posted on 2013-01-21
3
Medium Priority
?
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

762 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