?
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
Medium Priority
?
507 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 750 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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