?
Solved

create a simple formatted Word document or text.

Posted on 2006-06-13
6
Medium Priority
?
186 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
[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
  • 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 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Industry Leaders: 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 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