【问题标题】:Cmd on VBA code error " this object does not support this property or method"VBA 代码上的 Cmd 错误“此对象不支持此属性或方法”
【发布时间】:2012-12-21 04:02:03
【问题描述】:

在这里我使用的代码我尝试运行 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

【问题讨论】:

  • 你不能在 VBA IDE 中查看代码并告诉我们错误发生在哪里吗?
  • 在所有的 sendkey 和 cmd 工作完成后我就这样做了
  • .Refreshtype = xlOverwriteCells .TextFilePlatform = 874 在这里某处
  • 我发现它是 .refreshtype 如果我没有这一行,这非常重要,我的文本文件将在导入后开始将所有内容推到右侧
  • 如果您解决了您的问题,您可能希望发布您的解决方案作为答案或关闭帖子

标签: shell vba excel command-line batch-file


【解决方案1】:

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

在下面的示例中,我将演示如何将DIR 导出为summary2.txt 更改为适用。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多