2012-12-21 86 views
0

這裏我使用的代碼我嘗試運行cmdline創建txt文件,然後將其導入到Excel中它一直說「這個對象不支持這個屬性或方法」Cmd對VBA代碼錯誤「此對象不支持此屬性或方法」

我不知道在哪裏可以修復它我卡住了

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 
+3

您不能在VBA IDE中查看代碼並告訴我們錯誤在哪裏觸發? – nemmy

+0

我做到了周圍的一切sendkey和CMD工作完成後 – eathapeking

+0

.Refreshtype = xlOverwriteCells .TextFilePlatform = 874 在這裏的某個地方 – eathapeking

回答

2

Sendkeys是非常不可靠。打開後無需將命令窗口發送至命令窗口,即可輕鬆實現所嘗試的內容。請參閱下面的代碼。它減少了所有過多的代碼。

在下面的示例中,我正在演示如何導出DIRsummary2.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 
相關問題