Solved

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

Posted on 2013-10-23
8
282 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
8 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 27

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 27

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 46

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

813 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

10 Experts available now in Live!

Get 1:1 Help Now