Solved

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

Posted on 2013-10-23
8
279 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 45

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

746 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

9 Experts available now in Live!

Get 1:1 Help Now