Solved

Using Print #, Display line at center of user defined page width

Posted on 2004-04-15
4
200 Views
Last Modified: 2010-05-02
Hi,

I am reading one text file which have fixed width(80). This files have few lines which I am reading e.g Line01 = "I am swapnil"
Now my software should read this file and using print # method it should print it into another text file whose width is defined by user. User may select above 80 any width. Say 100 then
Line01 should get printed into new text file at the center of page width(100)

Can somebody give me code for center align???

How to do that?

Regards
swapnil
0
Comment
Question by:swapnilv
4 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 10839950
I guess you'll have to add spaces before you're string to center the text in your file...

dim CenterPos as integer
' centerpos =
' (linewidth / 2) -> give the middle of the line (center of page)
' line01 / 2 -> gives the middle of your string
CenterPos = (100 / 2) - round(len(line01) / 2,0)

Dim NewLine as string
for i = 1 to centerpos
   print " "
next i
print line01
0
 

Author Comment

by:swapnilv
ID: 10840276
Thanks man..

But the problem remains as it is.. I don't know how to proceed

the code is something like this..

Open TextFile For Output As #Numfile

Then I read line01

   Line01 = "I am swapnil"

    CenterPos = (100 / 2) - Round(Len(Line01) / 2, 0)

Dim NewLine As String
For Val1 = 1 To CenterPos
Print #Numfile, " "
Next
Print #Numfile, line01

It is printing with different lines in wordpad.whereas it should be in one line.

How to implement in one line.. I tried with spc(n) but I am not able to do with that also..I am not sure how to use spc().

Swapnil


dim CenterPos as integer
' centerpos =
' (linewidth / 2) -> give the middle of the line (center of page)
' line01 / 2 -> gives the middle of your string
CenterPos = (100 / 2) - round(len(line01) / 2,0)

Dim NewLine as string
for i = 1 to centerpos
   print " "
next i
print line01


0
 
LVL 5

Accepted Solution

by:
Xentor_ earned 250 total points
ID: 10844332
I believe you need a semicolon (;) after the Print #Numfile, " " statement.  That tells VB to stay on the same line in certain cases.

I'd actually reccommend doing that a bit differently

CenterPos = (PageWidth - Len(line01)) \ 2
'(Note the use of the backslash (\) instead of the forward slash to do integer division, ignoring fractions entirely)
Print #Numfile, Space(CenterPos) & line01

That should print CenterPos space characters, then your text.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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

19 Experts available now in Live!

Get 1:1 Help Now