Sub ChangeFileFormat(xltxFolder, xlsxFolder)
<span class="k">Dim</span> <span class="n">strCurrentFileExt</span> <span class="ow">As</span> <span class="kt">String</span>
<span class="k">Dim</span> <span class="n">strNewFileExt</span> <span class="ow">As</span> <span class="kt">String</span>
<span class="k">Dim</span> <span class="n">objFSO</span> <span class="ow">As</span> <span class="kt">Object</span>
<span class="k">Dim</span> <span class="n">objFolder</span> <span class="ow">As</span> <span class="kt">Object</span>
<span class="k">Dim</span> <span class="n">objFile</span> <span class="ow">As</span> <span class="kt">Object</span>
<span class="k">Dim</span> <span class="n">xlFile</span> <span class="ow">As</span> <span class="n">Workbook</span>
<span class="k">Dim</span> <span class="n">strNewName</span> <span class="ow">As</span> <span class="kt">String</span>
<span class="k">Dim</span> <span class="n">strXltxFolderPath</span> <span class="ow">As</span> <span class="kt">String</span>
<span class="k">Dim</span> <span class="n">strXlsxFolderPath</span> <span class="ow">As</span> <span class="kt">String</span>
<span class="k">Set</span> <span class="n">objFSO</span> <span class="o">=</span> <span class="n">CreateObject</span><span class="p">(</span><span class="s">"Scripting.FileSystemObject"</span><span class="p">)</span>
<span class="n">strCurrentFileExt</span> <span class="o">=</span> <span class="s">".xltx"</span>
<span class="n">strNewFileExt</span> <span class="o">=</span> <span class="s">".xlsx"</span>
<span class="n">strXltxFolderPath</span> <span class="o">=</span> <span class="n">ThisWorkbook</span><span class="p">.</span><span class="n">Path</span> <span class="o">&</span> <span class="s">"\"</span> <span class="o">&</span> <span class="n">xltxFolder</span> <span class="o">&</span> <span class="s">"\"</span>
<span class="n">strXlsxFolderPath</span> <span class="o">=</span> <span class="n">ThisWorkbook</span><span class="p">.</span><span class="n">Path</span> <span class="o">&</span> <span class="s">"\"</span> <span class="o">&</span> <span class="n">xlsxFolder</span> <span class="o">&</span> <span class="s">"\"</span>
<span class="k">If</span> <span class="k">Not</span> <span class="n">objFSO</span><span class="p">.</span><span class="n">FolderExists</span><span class="p">(</span><span class="n">strXltxFolderPath</span><span class="p">)</span> <span class="k">Then</span> <span class="c">'判断指定文件夹是否存在
MsgBox "【模板文件】文件夹不存在"
Exit Sub
End If
<span class="k">If</span> <span class="k">Not</span> <span class="n">objFSO</span><span class="p">.</span><span class="n">FolderExists</span><span class="p">(</span><span class="n">strXlsxFolderPath</span><span class="p">)</span> <span class="k">Then</span> <span class="c">'判断指定文件夹是否存在
fs.CreateFolder strXlsxFolderPath
End If
<span class="k">Set</span> <span class="n">objFolder</span> <span class="o">=</span> <span class="n">objFSO</span><span class="p">.</span><span class="n">getfolder</span><span class="p">(</span><span class="n">strXltxFolderPath</span><span class="p">)</span>
<span class="k">For</span> <span class="k">Each</span> <span class="n">objFile</span> <span class="ow">In</span> <span class="n">objFolder</span><span class="p">.</span><span class="n">Files</span> <span class="c">'循环所有的模板文件
strNewName = objFile.Name
If Right(strNewName, Len(strCurrentFileExt)) = strCurrentFileExt Then
Application.AskToUpdateLinks = False '关闭程序询问更新链接提示
Application.DisplayAlerts = False
ThisWorkbook.UpdateLinks = xlUpdateLinksAlways '更新链接
Set xlFile = Workbooks.Open(objFile.Path, , True) '读取模板文件
For Each sh In xlFile.Sheets '替换文件中的公式
sh.UsedRange.Value = sh.UsedRange.Value
Next
<span class="n">strNewName</span> <span class="o">=</span> <span class="n">Replace</span><span class="p">(</span><span class="n">strNewName</span><span class="p">,</span> <span class="n">strCurrentFileExt</span><span class="p">,</span> <span class="n">strNewFileExt</span><span class="p">)</span> <span class="c">'替换文件名为新的文件名
Select Case strNewFileExt
Case ".xlsx"
xlFile.SaveAs strXlsxFolderPath & strNewName, XlFileFormat.xlOpenXMLWorkbook '保存为不带宏的excel
Case ".xlsm"
xlFile.SaveAs strXlsxFolderPath & strNewName, XlFileFormat.xlOpenXMLWorkbookMacroEnabled '保存为带宏的excel
End Select
xlFile.Close
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
End If
Next objFile
ClearMemory:
strCurrentFileExt = vbNullString
strNewFileExt = vbNullString
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set xlFile = Nothing
strNewName = vbNullString
strFolderPath = vbNullString
End Sub