0
votes

I'm working on spacing on a word document and I need to vary the location of page breaks and number of blank paragraphs for spacing that the macro inputs based on the number of lines of a previous paragraph so that the 2 fields that are input by the ClientNameandDate sub are on the same page.

I've used the number of agents to vary the spacing, but the problem is that some agents' information takes up more space than others, and therefore the variable length paragraph may have a different number of lines even when the number of agents is the same. This results in the macro sometimes generating a blank page, which I don't want. The HIPAANumber variable is the one that stores how many agents there are on the form. I looked at the options when I write Selection. and Selection.Paragraph. but none of the options seem to capture the information I need.

With Selection
     If HIPAANumber > 2 And HIPAANumber < 5 Then
            .InsertBreak Type:=wdPageBreak
     End If

 Call ClientNameandDate(ClientName) 'The Sub inputs the Date and Client's Name fields. These fields always take up the same amount of space.
 .TypeParagraph
 If HIPAANumber <= 2 Then
      .InsertBreak Type:=wdPageBreak
 Else
      .TypeParagraph
 End If
End With


Private Sub ClientNameandDate(ClientName)

    Selection.TypeParagraph

    With Selection
        .ParagraphFormat.Alignment = wdAlignParagraphLeft
        .TypeText Text:="_______________________________" 'This code inputs the Date field
        .TypeText Text:=Chr(11)
        .TypeText Text:="Date"
        .TypeParagraph
        .Font.Size = 4
        .TypeText Text:=Chr(11)
        .Font.Size = 12
        .TypeText Text:=vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & "__________________________________________"
        .TypeText Text:=Chr(11)
        .TypeText Text:=vbTab & vbTab & vbTab & vbTab & vbTab & vbTab
        .Font.Bold = True
        .TypeText Text:=UCase(ClientName)

    End With
End Sub

'''

I don't really know how to use objects in VBA, so if someone could explain how to use objects for this, I would really appreciate it. I think the easiest way to do this is to capture the number of lines of text in the paragraph that has a variable length and then use that in an If statement later, but I don't know how to capture the number of lines of text of a paragraph.

1
Paragraph formatting might be a simple way to control the page break. Have you tried keep with next, keep lines together, and page break before, but ruled out paragraph formatting as a solution?scenography
My recommendation would be to solve this using Word's internal STYLE capability. One paragraph style for the first field/paragraph, another for the second. The first one should have the paragraph formatting "Keep with Next" along with "Keep lines together". The second would not have "Keep with Next" but would have "Keep together". This will ensure that the two paragraphs stay on the same page, no matter what. No macro required...Cindy Meister

1 Answers

0
votes

The Information property can return a line number, as in the following code.

Sub CountLinesInTheSelectedParagraph()
    Dim rngFirst As Range, rngLast As Range

    Set rngFirst = Selection.Range
    rngFirst.Expand Unit:=wdParagraph
    rngFirst.Collapse Direction:=wdCollapseStart

    Set rngLast = Selection.Range
    rngLast.Expand Unit:=wdParagraph
    rngLast.Collapse Direction:=wdCollapseEnd

    Debug.Print _
        rngLast.Information(Type:=wdFirstCharacterLineNumber) _
        - rngFirst.Information(Type:=wdFirstCharacterLineNumber) _
        & " lines in the selected paragraph"
End Sub