Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I reproduce the content and formatting of an VB6 MSFlexGrid to a text file?

Posted on 2013-10-23
8
Medium Priority
?
293 Views
Last Modified: 2013-12-05
I would like to know how I can send data from an MSFlexGrid to a text file where the formatting and the data itself has all the proper spacing just like the MSFlexGrid it came from. I just want to recreate a text file report of the MSFLexGrid data. Many thanks in advance for any help.
0
Comment
Question by:jazjef
7 Comments
 
LVL 5

Expert Comment

by:MohitPandit
ID: 39596914
Hello,

Kindly take a look over below code:

    Private Sub Form_Load()
       '~~> Declare variables.
       Dim Entry As String, i As Long
       
       MSFlexGrid1.FixedCols = 0
       MSFlexGrid1.FixedRows = 0
       
       MSFlexGrid1.Cols = 1
       '~~> To add 10 items
       For i = 1 To 10
          Entry = "This is item number : " & i
          MSFlexGrid1.AddItem Entry
       Next i
    End Sub
     
    Private Sub Command1_Click()
        Dim intRow As Integer, intCol As Integer
       
        '~~> Change to target File
        FileName = "C:\Test.Txt"
       
        iFreeFile = FreeFile
       
        '~~> Open File for output
        Open FileName For Output As iFreeFile
       
        '~~> Get the rows and Columns of Flex
        intCol = MSFlexGrid1.Cols
        intRow = MSFlexGrid1.Rows
     
        For i = 1 To intRow
            For j = 1 To intCol
                With MSFlexGrid1
                    '~~> Write to text file
                    Print #iFreeFile, .TextMatrix(i - 1, j - 1) & " "
                End With
            Next
        Next
     
        MsgBox "File Exported Successfully"
        Close #iFreeFile
    End Sub

Open in new window


Best Regards,
Mohit Pandit
0
 
LVL 4

Author Comment

by:jazjef
ID: 39597227
Thanks Mohit; here's what it looks like when I print my grid with the code...it only prints to a single column and the formatting is lost. JPG of actual msflexgrid and data is attached.

CIN1975
HOME_Player
K
BB
1B
2B
3B
HR
SB
RBI
AB
HOME_Pitcher
K
BB
H
HR
R
IP
 
P Rose
 
 
 
 
 
 
 
 
2
J Billingham
2
4
9
1
7
 
 
J Bench
 
 
 
 
 
 
 
 
2
bb-grid-data.jpg
0
 
LVL 28

Expert Comment

by:Ark
ID: 39599060
AFAIK text file doesn't support formatting. You can probably use vbTab, but I suggest using HTML and webbrowser instead of FlexGrid.
0
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!

 
LVL 15

Expert Comment

by:eemit
ID: 39601993
Try this:

Option Explicit

Private Sub Form_Load()
  Dim sEntry As String
  Dim nCols As Long
  Dim nRows As Long
  Dim nCol As Long
  Dim nRow As Long
  
  nCols = 16
  nRows = 10
  
  With MSFlexGrid1
     .FixedCols = 0
     .FixedRows = 0
  
     .Rows = 0
     .Cols = nCols
     
     For nRow = 0 To nRows - 1
         sEntry = ""
         For nCol = 0 To nCols - 1
             sEntry = sEntry & "Item: " & nRow & "-" & nCol & vbTab
         Next nCol
         
         ' Trim last vbTab
         sEntry = Left$(sEntry, Len(sEntry) - 1)
         
         .AddItem sEntry
     Next nRow
  
  End With
  
End Sub
 
Private Sub Command1_Click()
   Dim nCols As Long
   Dim nRows As Long
   Dim nCol As Long
   Dim nRow As Long
   
   Dim sLine As String

   Dim sFileName As String
   Dim nFreeFile As Long
  
   ' Change to target File
   sFileName = "C:\Test.Txt"
  
   nFreeFile = FreeFile
  
   ' Open File for output
   Open sFileName For Output As nFreeFile
  
   ' Get the rows and Columns of Flex
   With MSFlexGrid1
      nCols = .Cols
      nRows = .Rows
   
      For nRow = 0 To nRows - 1
          sLine = ""
          For nCol = 0 To nCols - 1
              ' Write to text file
              sLine = sLine & .TextMatrix(nRow, nCol) & vbTab
          Next nCol
          ' Trim last vbTab
          sLine = Left$(sLine, Len(sLine) - 1)
          Print #nFreeFile, sLine
      Next nRow
   
   End With
  
   MsgBox "File Exported Successfully"
   Close #nFreeFile
End Sub

Open in new window

0
 
LVL 28

Expert Comment

by:Ark
ID: 39602155
vbTab is nothing more then moving carret to next tab position (32 dialog units ~ 8 chars by default), ie
vbTab & a
means a will be printed at 8th position
vbTab & a & vbTab & b - a at 8th and b at 16th
but what if a or b is longer then 8 chars? vbTab moves carret to NEXT stop, so entire 'Table' will be corrupted. If you need formatted text - choose smth else then *.txt
0
 
LVL 4

Accepted Solution

by:
jazjef earned 0 total points
ID: 39635062
I found a solution perusing code on the internet. I finally gave up and went back to using a textbox instead of a RichTextBox---but I still wanted to write the contents to HTML. Here's the code that solves the problem:

Open SaveFileDialog1.FileName For Output As #iFree
Dim HTDOC As Variant
Dim CurLin
Dim FinalHTML As String
    HTDOC = TextBox1.Text
    HTDOC = Split(HTDOC, vbNewLine)
    DoEvents
    For i = 0 To UBound(HTDOC)
        CurLin = HTDOC(i)
        FinalHTML = FinalHTML & CurLin & "<br>"
    Next i
    DoEvents
    Print #iFree, FinalHTML
Close #iFree
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39697793
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Viewers will learn the different options available in the Backstage view in Excel 2013.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

876 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