Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何将浏览过的文本文件放入VBA列表框中_Vb.net_Ms Access_Vba - Fatal编程技术网

Vb.net 如何将浏览过的文本文件放入VBA列表框中

Vb.net 如何将浏览过的文本文件放入VBA列表框中,vb.net,ms-access,vba,Vb.net,Ms Access,Vba,因此,我对使用access/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. '

因此,我对使用access/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是我试图将文本文件放入的列表框。 谢谢你的帮助
谢谢

我想你的问题是线路问题

file = fdlg
应该是

file = fdlg.SelectedItems(1)
内联添加的注释:

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
这将防止您意外输入错误的变量名称


如果您单击“工具/选项”,然后在编辑器选项卡中选择“需要变量声明”,则默认情况下可以让VBA项目添加此行。

您具体遇到了什么问题?好的,效果很好!非常感谢。我想知道是否有一种方法可以实现特征线类型设置。就像在行输入过程中,它发现了一个特定的字符,它将在那里结束行并移动到下一行。或者文本文件必须像那样被分离出来吗?我不确定我是否真的理解了你所说的,但是任何类型的文本解析都是可能的。您可以将文本文件读入单个变量,然后使用split命令将其拆分为数组。然后遍历数组。本网站有一个将文本文件读入变量的示例:搜索“函数GetText”