2012-12-10 25 views
0

我嘗試將保存在personl.xls中的VBA宏應用於給定目錄中的所有文件,但是我在第29行中遇到錯誤.. 恐怕我在這裏弄到了一些東西:VBS將Excel VBA宏應用於當前目錄中的所有文件

Option Explicit 
On Error Resume Next 

Dim xlApp 
Dim xlBook 

Dim No_Of_Files 
Dim i 
Dim File_Path 

Set xlApp = CreateObject("Excel.Application") 
xlApp.DisplayAlerts = True 

File_Path = "C:\Dokumente und Einstellungen\kcichini\Eigene Dateien\Stuff\Test\" 

With xlApp.FileSearch 
    .NewSearch 
    .LookIn = File_Path 
    .Filename = "*.xls" 
    .SearchSubFolders = False 
    .Execute 

    No_Of_Files = .FoundFiles.Count 

    For i = 1 To No_Of_Files 
    Set xlBook = xlApp.Workbooks.Open(.FoundFiles(i), 0, False) 
    xlApp.Run "'C:\Dokumente und Einstellungen\kcichini\Anwendungsdaten\Microsoft\Excel\XLSTART\PERSONL.XLS'!SASXLSFormat" 
    xlApp.ActiveWorkbook.Close 
    Next i 

End With 

xlApp.Quit 
Set xlBook = Nothing 
Set xlApp = Nothing 
+0

什麼是第29行?那錯誤在說什麼? – jagsler

+0

這是'下一個我'。留言(德語):'Anweisungsende erwartet'(?en:期待命令結束?) – Kay

+1

@Kay - 「Next i」不是VBScript,使用普通的「Next」。 –

回答

0

我明顯是在一個完全錯誤的軌道上。 但這似乎工作正常:

Option Explicit 
On Error Resume Next 

Dim xlApp 
Dim xlBook 
Dim sPath 

Dim fso 
Dim ObjFolder 
Dim ObjFiles 
Dim ObjFile 

'make an object with the excel application 
Set xlApp = CreateObject("Excel.Application") 
xlApp.DisplayAlerts = True 

'Creating File System Object 
Set fso = CreateObject("Scripting.FileSystemObject") 

'Getting the Folder Object 
Set ObjFolder = fso.GetFolder("C:\Dokumente und Einstellungen\kcichini\Eigene Dateien\Stuff\Test") 

'Getting the list of Files 
Set ObjFiles = ObjFolder.Files 

'Running the macro on each file 
For Each ObjFile In ObjFiles 
    'MsgBox (ObjFolder & "\" & ObjFile.Name) 
    Set xlBook = xlApp.Workbooks.Open(ObjFolder & "\" & ObjFile.Name, 0, False) 
    xlApp.Run "'C:\Dokumente und Einstellungen\kcichini\Anwendungsdaten\Microsoft\Excel\XLSTART\PERSONL.XLS'!SASXLSFormat" 
    xlApp.xlBook.Close 
Next 

xlApp.Quit 
Set xlBook = Nothing 
Set xlApp = Nothing 
相關問題