从内部保存带有文件名的Excel文件

时间:2017-01-04 07:07:13

标签: excel vbscript

我正在使用以下脚本将“.xls”文件转换为“.xlsx”。我想将新转换的文件的文件名更改为在每个工作表的单元格“G9”中找到的单元格值。请在脚本中建议更改,以便将文件名更改为“G9”的单元格值。

Option Explicit

Dim oFSO, myFolder
Dim xlText

myFolder="C:\Users\khawar\Downloads"


Set oFSO = CreateObject("Scripting.FileSystemObject")
xlText = 51 'Excel Text format enum
Call ConvertAllExcelFiles(myFolder)
Set oFSO = Nothing

Call MsgBox ("Done!")


Sub ConvertAllExcelFiles(ByVal oFolder)
Dim targetF, oFileList, oFile
Dim oExcel, oWB, oWSH

    Set oExcel = CreateObject("Excel.Application")
    oExcel.DisplayAlerts = False
    Set targetF = oFSO.GetFolder(oFolder)
    Set oFileList = targetF.Files
    For Each oFile in oFileList
        If (Right(oFile.Name, 4) = ".xls") Then
            Set oWB = oExcel.Workbooks.Open(oFile.Path)     
            oWB.ActiveSheet.Name = "Data"
            For Each oWSH in oWB.Sheets
                Call oWSH.SaveAs (myFolder & "\" & oWB.Name & ".xlsx", xlText)
            Next
            Set oWSH = Nothing
            Call oWB.Close
            Set oWB = Nothing
        End If
    Next
    Call oExcel.Quit
    Set oExcel = Nothing

End Sub

0 个答案:

没有答案