create a simple formatted Word document or text.

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!
LVL 2
José PerezAsked:
Who is Participating?
 
Mark_FreeSoftwareConnect With a Mentor Commented:

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
 
Mark_FreeSoftwareCommented:


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
 
José PerezAuthor Commented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
José PerezAuthor Commented:
Excellent!

many thanks!

B!
0
 
Mark_FreeSoftwareCommented:


thanks for the points, and happy coding!
0
 
José PerezAuthor Commented:
thanks to you!

and God bless you :)

Oscar.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.