[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Help with Loops

Posted on 2013-01-21
3
Medium Priority
?
145 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

Hire Technology Freelancers with Gigs

Work with 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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

649 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