Solved

create a simple formatted Word document or text.

Posted on 2006-06-13
6
174 Views
Last Modified: 2010-04-30
i have a form with several textboxes (10).

i need to print this values in specific positions, it is for a invoice printing app. the result file will be printed with the values only, no images, no backgrounds, just values.

the program does not save any info into any database.

i think the best choice can be to create a Word document and write the information on specific coordenates (x,y). maybe another choice can be create a notepad text file and write into this file, but i dont know how to position the values from the textboxes.

any ideas can help and if any sample source is avaailable, please tell it. and as faster as i can apply a solution.

points will be assigned to the best simple choice.

many, many thanks,

B!
0
Comment
Question by:José Perez
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16898345


take a textbox

set the font to courier (or courier new)


now a space is the same width as every letter.
formatting is really easy now

example: (left and right alignment!)

create a new form, with a button (command1) and a textbox (text1)
make sure text1 is multiline!

now paste this code, run the program and press the button


Option Explicit

Private Sub Command1_Click()
   Text1.Text = "Left aligned example:" & vbCrLf
   Text1.Font = "Courier new"
   Text1.FontSize = 8
   Text1.Width = 6855
   Text1.Height = 3975
   align_Left "1", "2", "3", "4", "5"
   align_Left "5", "4", "3", "2", "1"
   align_Left "one", "two", "three", "four", "five"
   align_Left "11", "22", "33", "44", "55"
   Text1.Text = Text1.Text & vbCrLf & vbCrLf & "Right aligned example:" & vbCrLf
   align_Right "1", "2", "3", "4", "5"
   align_Right "5", "4", "3", "2", "1"
   align_Right "one", "two", "three", "four", "five"
   align_Right "11", "22", "33", "44", "55"
End Sub

Private Sub align_Left(x1 As String, x2 As String, x3 As String, x4 As String, x5 As String)
Const CharField As Long = 10   'size of each field
Dim tmp As String
   tmp = x1 & Space$(CharField - Len(x1))
   tmp = tmp & x2 & Space$(CharField - Len(x2))
   tmp = tmp & x3 & Space$(CharField - Len(x3))
   tmp = tmp & x4 & Space$(CharField - Len(x4))
   tmp = tmp & x5 & Space$(CharField - Len(x5))
   Text1.Text = Text1.Text & tmp & vbCrLf
End Sub

Private Sub align_Right(x1 As String, x2 As String, x3 As String, x4 As String, x5 As String)
Const CharField As Long = 10   'size of each field
Dim tmp As String
   tmp = Space$(CharField - Len(x1)) & x1
   tmp = tmp & Space$(CharField - Len(x2)) & x2
   tmp = tmp & Space$(CharField - Len(x3)) & x3
   tmp = tmp & Space$(CharField - Len(x4)) & x4
   tmp = tmp & Space$(CharField - Len(x5)) & x5
   Text1.Text = Text1.Text & tmp & vbCrLf
End Sub
0
 
LVL 2

Author Comment

by:José Perez
ID: 16902421
ok, i am understanding, this code is very good, however, i would really like if you code explain a little bit more about the "spaces" usage, just comment, please, for me to understand better.

many thanks,

B!
0
 
LVL 13

Accepted Solution

by:
Mark_FreeSoftware earned 150 total points
ID: 16902722

ok, i explained how it works,
just ask if you aren't sure about anything:


Option Explicit

Private Sub Command1_Click()
   'format the textbox with the good font and stuff
   Text1.Text = "Left aligned example:" & vbCrLf
   Text1.Font = "Courier new"
   Text1.FontSize = 8
   Text1.Width = 6855
   Text1.Height = 3975
   
   
   'fill the textbox with some random data
   align_Left "1", "2", "3", "4", "5"
   align_Left "5", "4", "3", "2", "1"
   align_Left "one", "two", "three", "four", "five"
   align_Left "11", "22", "33", "44", "55"
   Text1.Text = Text1.Text & vbCrLf & vbCrLf & "Right aligned example:" & vbCrLf
   align_Right "1", "2", "3", "4", "5"
   align_Right "5", "4", "3", "2", "1"
   align_Right "one", "two", "three", "four", "five"
   align_Right "11", "22", "33", "44", "55"
End Sub

Private Sub align_Left(x1 As String, x2 As String, x3 As String, x4 As String, x5 As String)
Const CharField As Long = 10   'size of each field
Dim tmp As String
   tmp = x1             '' add the first string
   'the function Space(x) gives an x number of spaces
   'what is happening here:
   'Space$(CharField - Len(x1))
   'i take the maximum number of chars (10 as declared above, CharField)
   'now i subtract the length of x1 from that
   'this is the whitespace to fill
   'example:
   'x1="Hello"
   '10 - len("hello") = 5
   'so you can write the next uncommented line as:
   'tmp = tmp & Space$(5)
   tmp = tmp & Space$(CharField - Len(x1))
   'and repeat with 2,3,4,5 (on one line to reduce size)
   tmp = tmp & x2 & Space$(CharField - Len(x2))
   tmp = tmp & x3 & Space$(CharField - Len(x3))
   tmp = tmp & x4 & Space$(CharField - Len(x4))
   tmp = tmp & x5 & Space$(CharField - Len(x5))
   Text1.Text = Text1.Text & tmp & vbCrLf
End Sub

Private Sub align_Right(x1 As String, x2 As String, x3 As String, x4 As String, x5 As String)
Const CharField As Long = 10   'size of each field
Dim tmp As String
   
   'here is the remaining length already calculated and filled first, so the right sides are aligned:
   'five spaces: (10-len(hello))    and the next one seven spaces (10-len(one)
   '     Hello
   '       one
   tmp = Space$(CharField - Len(x1))
   'now add the string itself:
   tmp = tmp & x1
   'and repeat with 2,3,4,5 (on one line to reduce size)
   tmp = tmp & Space$(CharField - Len(x2)) & x2
   tmp = tmp & Space$(CharField - Len(x3)) & x3
   tmp = tmp & Space$(CharField - Len(x4)) & x4
   tmp = tmp & Space$(CharField - Len(x5)) & x5
   Text1.Text = Text1.Text & tmp & vbCrLf
End Sub

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:José Perez
ID: 16903040
Excellent!

many thanks!

B!
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16903441


thanks for the points, and happy coding!
0
 
LVL 2

Author Comment

by:José Perez
ID: 16903509
thanks to you!

and God bless you :)

Oscar.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…

911 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