Solved

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

Posted on 2004-04-15
4
203 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 67
MsgBox 2 59
Macro Excel - Multiple If conditions 2 81
Added a column screws up code 5 63
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

856 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