Solved

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

Posted on 2013-06-26
6
491 Views
Last Modified: 2013-06-26
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?
0
Comment
Question by:Animaldrummer04
  • 3
  • 3
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39279511
0
 

Author Comment

by:Animaldrummer04
ID: 39279536
@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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39279546
If you change this

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

to

count = count + 1

Then it should count blank lines.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Animaldrummer04
ID: 39279561
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 39279585
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
 

Author Closing Comment

by:Animaldrummer04
ID: 39279678
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

Featured Post

A Knowledge Base That Stays Up-to-Date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

760 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

21 Experts available now in Live!

Get 1:1 Help Now