0

在这里,我使用的代码我尝试运行 cmdline 来创建 txt 文件,然后将其导入到 excel 中,它一直在说“这个对象不支持这个属性或方法”

我不知道在哪里修复它我现在卡住了

Sub ImportTextFile()  
Dim rPaht As String
Dim rFileName As String
Dim rPaht1 As String
Dim rFileName1 As String

txtFpath = Sheet1.Range("a1").Value
Filesum = "type unixinv* > summary2.txt"

ChDrive "D"
RSP = Shell(Environ$("COMSPEC"), vbNormalFocus)


Application.Wait Now + TimeValue("00:00:03")
SendKeys "CD " & txtFpath & "{ENTER}", True


Application.Wait Now + TimeValue("00:00:04")
SendKeys Filesum & "{ENTER}", True


Application.Wait Now + TimeValue("00:00:04")
SendKeys "exit " & "{ENTER}", True

rPaht = Sheet1.Range("a1")
rFileName = Sheet1.Range("a2")
Sheet1.Cells.Clear
With Sheet4.QueryTables.Add(Connection:= _
    "TEXT;" & rPaht & "\" & rFileName & ".txt", Destination:=Sheet1.Range("$A$4"))
    .Name = Sheet1.Range("C8").Value
    .TextFilePlatform = 874
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileOtherDelimiter = ":"
    .Refresh BackgroundQuery:=False
End With
Sheet1.Range("a1") = rPaht
Sheet1.Range("a2") = rFileName
End Sub
4

1 回答 1

2

Sendkeys非常不可靠。您正在尝试的内容可以轻松实现,而无需在打开命令窗口后将密钥发送到命令窗口。请参阅下面的代码。它减少了所有多余的代码。

在下面的示例中,我将演示如何在适用的情况下导出DIRsummary2.txtChange。

Sub ImportTextFile()
    Dim txtFpath As String, Filesum As String
    Dim RSP

    'Ex: txtFpath = "D:\MyFolder"
    txtFpath = Sheet1.Range("a1").Value

    Filesum = "cmd.exe /c Type " & txtFpath & "summary2.txt"

    RSP = Shell(Filesum, vbHide)

    rPaht = Sheet1.Range("a1")
    '
    '~~> Rest of your code
    '
End Sub
于 2013-10-12T12:26:04.520 回答