How Can I Write an "If Statement" in Microsoft Word 2010 VBA Based on Number of Lines Used?

I'm guessing this is a fairly easy question to answer, but I just put in my 10 minutes of Google searching and I know someone here has to know it off the top of their heads.

I'm trying to write an "If Statement" in Microsoft Word 2010 VBA that's dependent on the number of lines used. I have something that might work but I haven't tested it yet because I don't have enough of the code written to run yet and I just wanted to verify that what I'm trying to do is possible (and the correct way to do it). This is what I have so far:

Sub printMe()
Dim Denials As Document
Dim lineCount As Long

Set Denials = Documents("Home Denial Letters.docm")
lineCount = Denials.ComputeStatistics(wdStatisticLines)

If lineCount > 48 And lineCount < 56 Then
Denials.FitToPages
End If
With Denials
.PageSetup.FirstPageTray = wdPrinterLowerBin
.PageSetup.OtherPagesTray = wdPrinterMiddleBin
End With

End Sub

Open in new window


Basically what I'm trying to do is shrink the document to fit on one page if it can be done without making the font unreasonably small. A document that's 55 lines long at 11pt font (default) ends up at 9.5pt when shrunk to fit on one page. If the document is longer than 55 lines then do not shrink it, and set the first page to print on letterhead (Lower Bin) and all other pages to print on normal paper (Middle Bin).

Is how I am going about this going to work, or is there a better way?
Animaldrummer04Asked:
Who is Participating?
 
CodeCruiserCommented:
Sorry about the confusion. To confirm

If line count is less than 55 then shrink the page
if line count is greater than 55 then don't shrink the page and instead print first page on letter head and other pages on normal paper

if that's correct then change you code to following

If lineCount > 48 And lineCount < 56 Then
Denials.FitToPages
Else
With Denials
.PageSetup.FirstPageTray = wdPrinterLowerBin
.PageSetup.OtherPagesTray = wdPrinterMiddleBin
End With
End If
0
 
CodeCruiserCommented:
0
 
Animaldrummer04Author Commented:
@CodeCruiser, not really. I know that the "Denials.ComputeStatistics(wdStatisticLines)" line works to count the number of total lines; I need the number to include any blank lines because really what I'm after is the last used line (which would be the total number of lines).

My question is more geared to the

If lineCount > 48 And lineCount < 56 Then

Open in new window


section of code. Is that written properly to trigger if the number of lines is between 49 and 55?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
CodeCruiserCommented:
If you change this

If (char > 0) Then
count = count + 1
End If

to

count = count + 1

Then it should count blank lines.
0
 
Animaldrummer04Author Commented:
CodeCruiser, I think either I'm not understanding you or i'm not explaining my question clear enough.

If I understand the code in the link (and making the modification that you suggested above), that function returns the number of lines in the Word document.

My code already returns the number of lines in the Word document, I don't need assistance with that part. I just need to know if I am writing the "If Statement" using the line total I already have correctly.
0
 
Animaldrummer04Author Commented:
Thank you, you confirmed I was doing it right. The reason I have it written the way I do is because the user might add on to the document at a later time (after the initial creation). Even if the document is to be shrunk to one page I still want the page setup to be so that the first page is on letterhead and the other pages are on regular paper - if there are no other pages it doesn't hurt anything, and if the document is lengthened at a future point it will be set up to print the right way without the user having to mess with it.

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.