Link to home
Start Free TrialLog in
Avatar of Animaldrummer04
Animaldrummer04Flag for United States of America

asked on

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?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of Animaldrummer04

ASKER

@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?
If you change this

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

to

count = count + 1

Then it should count blank lines.
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.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!