2011-05-06 60 views
0

因此,我是使用access/VBA的新手,並且無法正常工作。如何將瀏覽的文本文件並將其放入列表框VBA

Private Sub Get_File_Click() 

    Dim fdlg As Office.FileDialog 
    Dim pipe_file As Variant 
    Dim FileName As String 
    Dim file As String 
    Dim fn As Integer 

    ' Clear contents of listboxes and textboxes. ' 
    Me.OrigFile.RowSource = "" 
    Me.ConvertFile.RowSource = "" 
    Me.FileName = "" 

    ' Set up the File dialog box. ' 
    Set fdlg = Application.FileDialog(msoFileDialogFilePicker) 
    With fdlg 
     .AllowMultiSelect = False 

     ' Set the title of the dialog box. ' 
     .Title = "Select pipe delimited file" 

     ' Clear out the current filters, and then add your own. ' 
     .Filters.Clear 
     .Filters.Add "Text Files", "*.txt" 


     ' Show the dialog box. If the .Show method returns True, the ' 
     ' user picked a file. If the .Show method returns   ' 
     ' False, the user clicked Cancel.       ' 
     If .Show = True Then 
      file = fdlg 
      fn = FreeFile 
      Open file For Input As #fn 
      Do While Not EOF(fn) 
       Line Input #fn, pipe_file 
       Me.OrigFile.AddItem pipe_file 
      Loop 
     Else 
      MsgBox "You clicked Cancel in the file dialog box." 
     End If 
    End With 
End Sub 

這是我到目前爲止。 origFile是我試圖放入文本文件的列表框。 任何幫助表示讚賞 感謝

+0

你有什麼特別的問題? – theninjagreg 2011-05-06 18:34:28

回答

0

評論添加內聯:

Private Sub Get_File_Click() 

    Dim fdlg As Office.FileDialog 
    Dim pipe_file As Variant 
    'Why two vars named 'FileName' and 'file'? Since they are both string, assuming just one of these will do. 
    Dim FileName As String 
    'Dim file As String 
    Dim fn As Integer 
    'Need variant variable to get file name 
    Dim varFile As Variant 

    Me.OrigFile.RowSource = "" 
    Me.ConvertFile.RowSource = "" 

    'Don't use ME here. Unless you have an object named FileName (which I'm not sure why you would in this case) 
    'Me.FileName = "" 
    FileName = "" 

    Set fdlg = Application.FileDialog(msoFileDialogFilePicker) 
    With fdlg 
     .AllowMultiSelect = False 
     .Title = "Select pipe delimited file" 
     .Filters.Clear 
     .Filters.Add "Text Files", "*.txt" 

     If .Show = True Then 
      'Never used this code before but this is how you get the file name: 
      'Seems lame to have three lines of code to get one file name, but I guess this is the way this control works 
      For Each varFile In .SelectedItems 
       FileName = varFile 
      Next varFile 

      'The invalid code below was causing the error and it is no longer necessary. 
      'However, also wanted to point out that you are already in a With block for fldg so the fdlg object is not required 

      'FileName = fdlg.SelectedItems 

      fn = FreeFile 'FreeFile = Good! 

      'Commented out the line below because file is not used 

      'Open file For Input As #fn 
      Open FileName For Input As #fn 
      Do While Not EOF(fn) 
       Line Input #fn, pipe_file 
       Me.OrigFile.AddItem pipe_file 
      Loop 

      'Make sure to close the file too! 
      Close #fn 
     Else 
      MsgBox "You clicked Cancel in the file dialog box." 
     End If 
    End With 
End Sub 

此外,還有一個最後的技巧,確保你有以下代碼行分別位於模塊的頂部:

Option Explicit 

這將防止您無意中錯誤地鍵入變量的名稱。

如果您單擊「工具/選項」,然後在編輯器選項卡中選擇「需要變量聲明」,您可以默認添加此行。

+0

好吧,它很好用!謝謝。我想知道是否有一種方法可以實現分隔線類型設置。就像在線路輸入中找到一個特定的字符一樣,它會在那裏結束並移到下一行。或者文本文件必須已經被分離出來嗎? – James213 2011-05-07 04:09:29

+1

我不太確定我是否遵循你所說的,但任何類型的文本解析都是可能的。您可以將文本文件讀入單個變量,然後使用split命令將其分隔成一個數組。然後遍歷數組。本網站有一個將文本文件讀入變量的示例:http://www.exceluser.com/explore/questions/vba_textcols.htm搜索「函數GetText」 – ray 2011-05-07 16:11:38

0

我覺得你的問題是該行

file = fdlg 

應該

file = fdlg.SelectedItems(1) 
相關問題