J'ai actuellement un script qui utilise FSO, mais il ouvre aussi les fichiers .xlsm dans mon répertoire de travail. Je voudrais seulement ouvrir les fichiers .txt.Comment puis-je m'assurer d'ouvrir uniquement des fichiers texte lorsque je travaille avec FSO - VBA
Je trouve ce code qui devrait fonctionner, mais je ne peux pas comprendre comment l'appliquer à ma situation:
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
Mon code (Works, mais ouvre également .xlsm les fichiers, que je ne veux pas faire):
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 - Essayé, malheureusement ne ressemble pas à son supporté :(. Aussi aucune mention dans les fichiers d'aide de Microsoft: https://msdn.microsoft.com/en-us/library/f1xtf7ta(v = vs.84) .aspx – sikorloa
@Thierry - Comment créer une condition qui fait référence au nom de fichier? J'essaye If fso.GetFileName (fichier) <> "* .txt" Alors, mais ça ne fonctionne pas correctement – sikorloa
I ' a supprimé le commentaire et l'a mis comme réponse – Thierry