Solved

Help with Loops

Posted on 2013-01-21
3
138 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 2016 User Guides 5 36
ms office troubleshooting for users 8 42
Outlook 2012 VBA: Object missing 14 29
Word Perfect Question 11 17
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This article will show you how to use shortcut menus in the Access run-time environment.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

813 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

10 Experts available now in Live!

Get 1:1 Help Now