我是新手,不知道有没有简单的办法,有的话希望高手告诉我。 我用文本文件来保存和打开。 Option Explicit Dim FileName As String
Private Sub Command1_Click() CommonDialog1.Filter = "文本文档(*.txt)|*.txt" CommonDialog1.ShowOpen FileName = CommonDialog1.FileName Dim ro, cl As Long Dim t As String Dim h As String Dim s As String ro = 0 cl = 0 MSHFlexGrid1.Rows = Getrows(FileName) MSHFlexGrid1.Cols = Getcols(FileName) Open FileName For Input As #1 Len = 1000 While ro <> MSHFlexGrid1.Rows Line Input #1, s If Mid(s, 1, 1) <> "#" Then For cl = 0 To MSHFlexGrid1.Cols - 1 h = Trim(s) t = GetMyWord(h, cl + 1) If t = "#" Then t = "" MSHFlexGrid1.TextMatrix(ro, cl) = t Next cl ro = ro + 1 End If Wend Close #1 StatusBar1.Font = "黑体" StatusBar1.SimpleText = "当前词数:" StatusBar1.SimpleText = StatusBar1.SimpleText & CStr(MSHFlexGrid1.Rows - 1) & "个" End Sub
Private Function Getrows(FileName2 As String) As Integer Dim p, aa As Integer Dim s As String p = 0 On Error GoTo a: b: Open FileName For Input As #1 Len = 1000 While Not EOF(1) Line Input #1, s If Mid(s, 1, 1) <> "#" Then p = p + 1 End If Wend Close #1 Getrows = p Exit Function a: aa = MsgBox("没有选取文件", vbYesNo, "提示") If aa = vbYes Then Command1.Value = True GoTo b: End If
End Function
Private Function Getcols(FileName2 As String) As Integer Dim p As Integer Dim s As String p = 1 On Error Resume Next Open FileName For Input As #1 Len = 1000 Line Input #1, s Close #1 s = Trim(s) Do While InStr(s, " ") <> 0 s = Mid(s, InStr(s, " ")) s = Trim(s) p = p + 1 Loop Getcols = p End Function
Private Function GetMyWord(instring As String, j As Long) As String Dim i As Integer Dim st As String st = " " i = 0 Do While st <> "" instring = Trim(instring) st = Left(instring, InStr(instring, " ")) st = Trim(st) i = i + 1 If st = "" Then GetMyWord = instring Exit Function End If instring = Mid(instring, InStr(instring, " ")) instring = Trim(instring) If i = j Then GetMyWord = st Exit Function End If Loop End Function
Private Sub Command2_Click() Dim FileName2 As String Dim i, j, dex As Integer CommonDialog1.Filter = "文本文档(*.txt)|*.txt" CommonDialog1.ShowSave FileName2 = CommonDialog1.FileName Open FileName2 For Output As #1 For i = 0 To MSHFlexGrid1.Rows - 1 If i = 1 Then For dex = 1 To 60 Print #1, "#"; Next dex Print #1, End If For j = 0 To MSHFlexGrid1.Cols - 1 If MSHFlexGrid1.TextMatrix(i, j) = "" Or MSHFlexGrid1.TextMatrix(i, j) = " " Then Print #1, "#", Else Print #1, MSHFlexGrid1.TextMatrix(i, j), End If Next j Print #1, Next i Close #1 End Sub