vba 用 CreateObject(“Excel.Application”) 另存指定路径
时间: 2024-09-27 07:04:37
浏览: 14
在VBA (Visual Basic for Applications) 中,`CreateObject("Excel.Application")` 是创建 Microsoft Excel 应用程序的实例,允许我们在脚本中控制Excel。如果你想通过这个对象另存工作簿到特定路径,可以按照以下步骤操作:
```vba
Sub SaveWorkbookToSpecificPath()
' 创建一个新的Excel应用实例
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
' 检查是否已经打开一个工作簿,如果没有,就创建一个新的
If xlApp.Workbooks.Count = 0 Then
Dim wb As Workbook
Set wb = xlApp.Workbooks.Add
Set wb = xlApp.ActiveWorkbook
End If
' 指定你要保存的工作簿路径
Dim filePath As String
filePath = "C:\path\to\your\file.xlsx" ' 替换为你需要的文件路径
' 使用SaveAs方法另存为新路径
wb.SaveAs Filename:=filePath, FileFormat:=xlExcelXMLSpreadsheet
' 关闭工作簿和退出Excel应
```
相关推荐

















