データベースだけではない7

Toidasu起動後最初にやることは、ユーザーが作成した問題文を読み込むことです。
ファイルを読み込み、表示するという一般的によく行われる処理です。
お手本通りと言えばそうなのですが、今回はCSVのような外部データを読み込んで1件ずつ処理をしたいわけではないので、一気にファイルを読み込む方法も、参考のためコメント行で示しておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
Private Sub 問題読込_Click() '### 問題テキストファイルを読み込み### '### テキストボックスに入れる処理 ### ' 'ヘルプを参考に書き換えれば仕上がります ' 'ダイアログを開きファイルを選択します。 ' ' Requires reference to Microsoft Office 11.0 Object Library. Dim fDialog As Office.FileDialog Dim varFile As Variant ' Set up the File Dialog. Set fDialog = Application.FileDialog(msoFileDialogOpen) With fDialog ' Allow user to make multiple selections in dialog box ' Set the title of the dialog box. . Title = "問題文を選択してください" ' Clear out the current filters, and add our own. .Filters.Clear .Filters.Add "問題ファイル", "*.txt" ' Show the dialog box. If the .Show method returns True, the ' user picked at least one file. If the .Show method returns ' False, the user clicked Cancel. If .Show = True Then 'Loop through each file selected and add it to our list box. varFile = .SelectedItems.Item(1) End If End With 'ここから読込処理開始 Open varFile For Input As #1 Do While Not EOF(1) Line Input #1, textline strData = strData & textline & vbCrLf Loop Close #1 '確認のためテキストボックスに表示する Me.Txt1 = strData 'ここからは一気に読み込む方法です ' Dim tmpStr As String ' Open GetFileName For Binary As #1 ' tmpStr = Space(LOF(1)) ' Get #1, , tmpStr ' tmpStr = Replace(tmpStr, vbNullChar, "") ' Close #1 ' Me.Txt1 = tmpStr '一気に読み込む方法はここまで 'この後は問題文を表示するための文字列操作処理がいろいろ続く 'それは次回以降に掲載予定 End Sub |
コメントを残す