Solved

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

Posted on 2013-10-23
8
284 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 47

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

749 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