是否可以使用没有安装Excel的vbs打开并保存xls文件?

时间:2017-09-07 15:04:37

标签: excel windows vbscript

如何在没有安装Excel的计算机上模仿此脚本的功能:

通过子文件夹循环1次 2.找到xls文件,然后打开,保存并关闭

Dim objFSO, objFolder, objFile
Dim objExcel, objWB
Set objExcel = CreateObject("Excel.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")

ShowSubfolders FSO.GetFolder("path\to\mainFolder"), 3

Sub ShowSubFolders(Folder, Depth)
    If Depth > 0 Then
        For Each Subfolder In Folder.SubFolders
            For Each objFile In Subfolder.Files
                If Right(objFile.Name, 3) = "xls" Then
                    Set objWB = objExcel.Workbooks.Open(objFile)
                    objWB.Save
                    objWB.Close
                End If
            Next

            ShowSubFolders Subfolder, Depth-1
        Next
    End If
End Sub

objExcel.Quit
Set objExcel = Nothing
Set objFSO = Nothing

1 个答案:

答案 0 :(得分:0)

Interop要求在计算机上安装Excel。

您需要使用其他Excel库,例如EasyXLS。 除了其他库,EasyXLS还有Excel component for VBS

您的代码如下所示:

Sub ShowSubFolders(Folder, Depth)
    If Depth > 0 Then
        For Each Subfolder In Folder.SubFolders
            For Each objFile In Subfolder.Files
                If Right(objFile.Name, 3) = "xls" Then
                    set objExcel = CreateObject("EasyXLS.ExcelDocument")
                    objExcel.easy_LoadXLSFile(objFile)
                    objExcel.easy_WriteXLSFile(objFileNew)
                End If
            Next

            ShowSubFolders Subfolder, Depth-1
        Next
    End If
End Sub