如何从word文件中保存excel文件并使用VBA选择路径?

时间:2016-06-30 19:21:28

标签: excel vba ms-word word-vba

我有一个宏从word文件中获取数据并将其写入excel文件并将其保存到特定位置。 我希望用户能够选择保存文件的位置。

这是我目前的代码:

 Sub createExcelFile()
        Dim mPathSave As String
        Dim xlsApp As Excel.Application
        Dim xlsWB As Workbook

        Set xlsApp = CreateObject("Excel.Application")
        Set xlsWB = xlsApp.Workbooks.Add

        'Want to make it dynamic'
        mPathSave = "C:\temp"

        callFunc = createExcel.createExcel(xlsApp, xlsWB)

        'Save the excel file
        xlsWB.SaveAs mPathSave & "\" & "teste" & ".xls", FileFormat:=56
        xlsWB.Close
        xlsApp.Quit
        MsgBox "Novo arquivo salvo em: " & mPathSave & "\" & "teste" & ".xls", vbInformation
    End Sub

我尝试使用Application.FileDialog打开对话框来选择地点,但我无法保存excel,它会打开以保存word文件。

1 个答案:

答案 0 :(得分:1)

这是一个简单的例子

Sub createExcelFile()
    Dim mPathSave As String
    Dim xlsApp As Excel.Application
    Dim xlsWB As Workbook

    Set xlsApp = CreateObject("Excel.Application")
    xlsApp.Visible = True
    Set xlsWB = xlsApp.Workbooks.Add

    'Want to make it dynamic'
    Application.FileDialog(msoFileDialogFolderPicker).Show
    mPathSave = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)

    'Save the excel file
    xlsWB.SaveAs mPathSave & "\" & "teste" & ".xls", FileFormat:=56
    xlsWB.Close
    xlsApp.Quit
End Sub