Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-15
4
Medium Priority
?
208 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
[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
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 750 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

670 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