vba中用于从目录编辑文件的过程调用无效或参数错误

vba中用于从目录编辑文件的过程调用无效或参数错误,vba,Vba,我有下面的代码,我已经最小化,只是为了保持重要的行。我在StrFile=Dir 下面的代码检查给定目录中的.py文件并向其中添加内容。它使用相同的名称在同一位置搜索.seq、.xml和.macro,并替换内容。但我得处理两个案子 对于“.xml”,有时它不存在,所以我需要跳过相关代码 “.macro”也是如此。有时它不存在,需要跳过它,有时它存在,对于后缀为_1,_2的相同测试用例,它可能是多个。我使用If条件处理了这两种情况。但要克服错误 谢谢你的帮助 Dim pypath, path, aN

我有下面的代码,我已经最小化,只是为了保持重要的行。我在
StrFile=Dir

下面的代码检查给定目录中的
.py
文件并向其中添加内容。它使用相同的名称在同一位置搜索
.seq
.xml
.macro
,并替换内容。但我得处理两个案子

  • 对于“.xml”,有时它不存在,所以我需要跳过相关代码
  • “.macro”也是如此。有时它不存在,需要跳过它,有时它存在,对于后缀为_1,_2的相同测试用例,它可能是多个。我使用
    If
    条件处理了这两种情况。但要克服错误
  • 谢谢你的帮助

    Dim pypath, path, aName, StrFile, history, sTemp1 As String
    
    Dim myFile          As String
    Sub InternalData_Header()
        
    path = "C:\Users\xyz\Downloads\2"
        
        testcount = 0
        
        ' Dim StrFile   As String
        StrFile = Dir(path & "\*.py")
        
        Do While Len(StrFile) > 0
            Dim sBuf    As String
            Dim sTemp   As String
            Dim iFileNum As Integer
            Dim sFileName As String
            
            ' Edit      as needed
            sFileName = path & "\" & StrFile
            iFileNum = FreeFile
            Open sFileName For Input As iFileNum
            sTemp = ""
            Do Until EOF(iFileNum)
                Line Input #iFileNum, sBuf
                sTemp = sTemp & sBuf & vbCrLf
            Loop
            Close iFileNum
            
            ''' Skip loop if autotest name is not found in a row
            If FoundCell Is Nothing Then
                'MsgBox "Autotest name Not found"
                'On Error GoTo ErrorHandler
            End If
            
            iFileNum = FreeFile
            Open sFileName For Output As iFileNum
            Print #iFileNum, sTemp
            Close iFileNum
            StrFile = Dir '''' Error at this line: Runtime error 5: Invalid procedure call or argument
            
            '''' Add history line in .seq files ===============================================
            sFileName = path & "\" & Arr1(0) & ".seq"
            
            Open sFileName For Input As iFileNum
            sTemp = ""
            Do Until EOF(iFileNum)
                Line Input #iFileNum, sBuf
                If InStr(1, sBuf, "   Initial") Then
                    seqHist = sBuf
                End If
                sTemp = sTemp & sBuf & vbCrLf
            Loop
            Close iFileNum
            
            iFileNum = FreeFile
            Open sFileName For Output As iFileNum
            Print #iFileNum, sTemp
            Close iFileNum
            
            '''' Add history line in .xml files ===============================================
            Dim strFileExists2 As String
            sFileName2 = path & "\" & Arr1(0) & "--master.xml"
            'On Error GoTo ErrorHandler
            strFileExists2 = Dir(sFileName2)
            If strFileExists2 = "" Then
                ''' .xml file does not exists, skip
            Else
                Open sFileName2 For Input As iFileNum
                Do Until EOF(iFileNum)
                    Line Input #iFileNum, sBuf1
                    sTemp1 = sTemp1 & sBuf1 & vbCrLf
                Loop
                ''' xml history line
                Close iFileNum
    
                iFileNum = FreeFile
                Open sFileName2 For Output As iFileNum
                Print #iFileNum, sTemp1
                Close iFileNum
            End If
            
            '''' Add history line in .macro files ===============================================
            Dim macroCount As Long
            Dim strFileExists1 As String
            macroCount = 1
            For macroCount = 1 To 5 ''' Check upto 5 macro files whether they are present, if yes,  make changes
                'sFileName = path & "\" & StrFile
                sFileName1 = path & "\" & Arr1(0) & "_" & macroCount & ".macro"
                strFileExists1 = Dir(sFileName1)
                
                If strFileExists1 = "" Then
                Else
                    iFileNum = FreeFile
                    Open sFileName1 For Input As iFileNum
                    sTemp = ""
                    Do Until EOF(iFileNum)
                    Loop
                    Close iFileNum
                    ' macro history line
                    
                    iFileNum = FreeFile
                    Open sFileName1 For Output As iFileNum
                    Print #iFileNum, sTemp
                    Close iFileNum
                End If
                
            Next macroCount
            macroCount = 1
            lcol = 0
        Loop
        
    End Sub
    

    使用下面的
    FileSystemObject
    方法,我可以解决这个错误

    Sub CheckFileExist()
    Dim MyFSO As FileSystemObject
    Set MyFSO = New FileSystemObject
        If MyFSO.FileExists("C:\Users\sumit\Desktop\Test\Test.xlsx") Then
            MsgBox "The File Exists"
        Else
            MsgBox "The File Does Not Exist"
        End If
    End Sub
    

    你也可以这样做。如果您正在迭代的文件夹中创建新文件,这将是首选的解决方案

    Dim pyFiles As New Collection
    Dim fnm as Variant
    
    fnm = Dir(path & "\*.py")
    do while fnm <> ""
        pyFiles.add path & "\" & fnm
        fnm = Dir
    loop
    
    for each fnm in pyfiles
        if Dir(fnm) <> "" then
            ' process file
        end if
    next pyfile
    
    Dim pyFiles作为新集合
    Dim-fnm作为变体
    fnm=Dir(路径&“\*.py”)
    当fnm“”时执行此操作
    pyFiles.add路径&“\”&fnm
    fnm=Dir
    环
    对于pyfile中的每个fnm
    如果目录(fnm)“,则
    '进程文件
    如果结束
    下一个pyfile
    
    当您在使用DIR枚举文件夹中的文件的中间时,您不能使用DIR检查文件是否存在。感谢NicholasHunter的提示。