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
505 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

724 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