Solved

create a simple formatted Word document or text.

Posted on 2006-06-13
6
173 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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

18 Experts available now in Live!

Get 1:1 Help Now