1

我在一个文件夹中有一张 Excel 表,并尝试使用以下代码将此文件夹中的某些文件合并到一个文件中:

Private Sub CommandButton1_Click()
  Dim RET As Variant
  RET = Shell("cmd.exe copy  files1.txt + file2.txt out.txt", 0)
End Sub

作为 RET 的返回值,我得到 1560。调试时没有错误,但也没有“out.txt”。我的代码有什么问题?谢谢

4

2 回答 2

2

我认为您错过了 cmd 参数和路径中的 /C 。

Private Sub CommandButton1_Click()
  Dim RET As Variant
  RET = Shell("cmd.exe /C copy  C:\Data\files1.txt + C:\Data\file2.txt C:\Data\out.txt", 0)
End Sub

返回值不等于0表示进程已启动(是实际的进程id)

于 2012-11-08T20:19:46.523 回答
1

VBA方式;

Function readFile(path) As String
    On Error GoTo ERR_IO
    Dim hF As Integer: hF = FreeFile
    Open path For Input As #hF
    readFile = Input$(LOF(hF), hF)
ERR_IO:
    Close #hF
End Function

Function writeFile(path, buffer) As Boolean
    On Error GoTo ERR_IO
    Dim hF As Integer: hF = FreeFile
    Open path For Output As #hF
    Print #hF, buffer
    writeFile = True
ERR_IO:
    Close #hF
End Function

Sub merge()
    Dim buffer As String
    buffer =          readFile("C:\xxx\files1.txt")
    buffer = buffer & readFile("C:\xxx\files2.txt")

    writeFile "c:\xxx\out.txt", buffer
End Sub
于 2012-11-09T12:03:01.000 回答