【问题标题】:How Can I Make Sure I Only Open Text Files When Working With FSO - VBA如何确保在使用 FSO 时只打开文本文件 - VBA
【发布时间】:2016-03-15 04:10:35
【问题描述】:

目前有一个使用 FSO 的工作脚本,但它也会在我的工作目录中打开 .xlsm 文件我只想打开 .txt 文件。

我发现这段代码应该可以工作,但是我不知道如何将它应用到我的情况:

Sub test()
    ' Loop thru all files in the folder
    folder = ActiveWorkbook.path

    path = folder & "\*.txt"

    Filename = Dir(path)

    Do While Filename <> ""
       'insert other functions here
    Loop

End Sub

我的代码(有效,但也会打开 .xlsm 文件,我不希望它这样做):

Option Explicit

Sub Initialize_barcode_lookup_Array_test()

 Dim fso As FileSystemObject
 Dim folder As String, path As String, count_txt_files As Long, Filename As String
 Dim folder2 As folder
 Dim file As file
 Dim FileText As TextStream
 Dim TextLine As String
 Dim Items() As String
 Dim ShippingPlanArray() As String
 Dim i As Long, j As Long, k As Long
 Dim cl As Range
 Dim fName
 Dim row As Long, column As Long

 Dim shipping_plan As Long      'Number of shipping plans text files imported
 Dim barcode_Lookup() As String
 Dim lastRow As Long
 Dim longest_lastRow As Long
 Dim counter As Long
 Dim FNSKU_Input As String

'<<<< Creating FSO Object >>>>>
    'Define longest_lastRow
    longest_lastRow = 0

    'Define i (References the text file open)
    i = 0

    ' Get a FileSystem object
    Set fso = New FileSystemObject

    ' get the directory you want
    Set folder2 = fso.GetFolder(ActiveWorkbook.path)

    ' Loop only while the files being opened are .txt files:

    For Each file In folder2.Files

        row = 0
        column = 0

        Set FileText = file.OpenAsTextStream(ForReading)
        Do Until FileText.AtEndOfStream

            fName = FileText.ReadLine
            'Parse data by tabs (text-tab delimited) into Items() array
            Items() = Split(fName, vbTab)

                ' Redimension Preserve the ShippingPlanArray()
                ' NOTE: You can only Redimension preserve the last dimension of a multi-dimensional array
                ' (In this case: row)
                ReDim Preserve ShippingPlanArray(9, row)

                'Read Data into an Array Variable
                For column = LBound(Items) To UBound(Items)
                'MsgBox Items(column)
                ShippingPlanArray(column, row) = Items(column)
                Next column

            row = row + 1
        Loop

    Next file



End Sub

【问题讨论】:

  • @Thierry - 试过了,不幸的是它看起来不支持:(。微软帮助文件中也没有提到它:msdn.microsoft.com/en-us/library/f1xtf7ta(v=vs.84).aspx
  • @Thierry - 你将如何创建一个引用文件名的条件?我正在尝试 If fso.GetFileName(file) "*.txt" 然后,但它不能正常工作
  • 我已删除评论并将其作为答案。
  • FSO 有一个 getextension() 方法可以用来检查文件类型。或使用If lcase(file.name) like "*.txt" then

标签: arrays vba excel fso


【解决方案1】:

我不知道 fso 是否支持 GetFolder 的重载方法,您可以在其中指定模式。如果是,请使用它,即 GetFolder(Path, "*.txt")。如果不是,您能否仅在“for each”循环中添加一个简单的条件来检查文件扩展名,并且只处理以“.txt”结尾的文件扩展名。

更新:

试试这个:

For Each file In folder2.Files
    Dim extension As String
    extension = LCase(Mid$(file, InStrRev(file, ".")))
    If extension = ".txt" Then
        Debug.Print "TEST"
    End If
Next

我已经对其进行了测试,它按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    相关资源
    最近更新 更多