Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
291 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
[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
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 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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

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.
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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…

721 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