We help IT Professionals succeed at work.

How do I send MSFlexGrid data to a tab-delimited text file in VB6 so that all the columns remain aligned?

Last Modified: 2012-06-27
I want to send the data from VB6 MSFlexGrid1 to a tab-delimited text file and preserve the column alignments. Anyone know how to do this? Thanks for any help...
Watch Question

Ok, first, tab delimited text files won't look like the columns are aligned unless you open in an app like Excel that can show columns properly.  Is that what you expect?

Regarding the flexgrid, is your data based on a record set or data you manually pushed into the control?
Kbirecki is correct.. Tabs in a plain text file don't automatically present the text in columns. Only the application used to view the text file can use the tabs to attempt to display it in columns.
Excel won't do it directly, you have to import the file into excel and confirm that it is a TAB file.
Other Apps like Wordpad still require you to set TAB widths manually..
What do you plan to use this file for? A Printed Report? or to email this file as an Invoice or Report?
This might help the ExEx folk give you some specific advice..


I would like it to be printable---but viewable as a text file type report so you don't need Excel in order to view it. The data would be manually pushed/entered into the control----there would be no recordset or datasource feeding it. What about the DataReports feature? Could you feed it from an MSFlexGrid? Are there any add-ons that will take the data of a grid and generate a simple report that you know of. Thanks for the responses.....

The MSFlexGrid is a display element.  I think whatever your source is should be the source for any other output.  Otherwise you;d have to write something that parses the contents of the flexgrid to spit them back out somewhere.  If you have the data to put into the flexgrid, just use that to push to your output format.

As for a text file output in columns, you also have to think about whether the viewer is using a monospace font or not.  Monospace fonts allow you to display text in columns, but only if you can be sure the recipient is using a viewer that is showing in monospace.
By using Printer.CurrentX and Printer.CurrentY you can print each cell text to exact column widths. You can even print vertical lines between the columns and a box around the whole format..
Then, by using one of the free PDF printer apps, you can direct that same printout to a PDF file which can be viewed by most PC users. The viewer is freely available.
Top Expert 2009
Unlock this solution and get a sample of our free trial.
(No credit card required)

VBClassicGuy, now that's a silver platter!  That looks like a lot of work.  Good job!


That's pretty elaborate vbclassicguy.... over my head; I don't know if I can implement that. I was thinking about something like this.... it will print an msflexgrid but without a vbcrlf somewhere if you print more than 1 then they print on top of each other. This will send an msflexgrid to Primo PDF or my HP Laserjet printer....

'0 = Primo PDF printer
'1 = print-to-file dialog
'2 = *.xps
'3 = ERROR
'4 = Laserjet 110
'5 = sendfax

Set Printer = Printers(0)
Printer.PaintPicture grd_VisStats.Picture, 0, 0



Nice solution VBClassicGuy... major-league coding. I have included another method I got to work in case anyone following this post is interested. Thanks to all....

'_____PRINT the gridview controls

CommonDialog3.PrinterDefault = True
CommonDialog3.CancelError = True

'_____Set flags - no page numbers, return the selected printer
CommonDialog3.flags = cdlPDReturnDC + cdlPDNoPageNums

'_____Enables error handling to catch cancel error
On Error Resume Next
'_____display the print dialog box

If Err Then
    '_____This code runs if the dialog was cancelled
    MsgBox "Printing Cancelled"
    Exit Sub
End If

'____Prints the msflexgrids
Printer.PaintPicture MSFlexGrid1.Picture, 500, 500
Printer.PaintPicture MSFlexGrid2.Picture, 500, 5000
Printer.PaintPicture MSFlexGrid3.Picture, 500, 9500

Top Expert 2009

Well, thank you , Sir. Glad to help out. Happy coding...
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.