简体   繁体   中英

How to specify the default directory for an Excel file SaveAs dialog?

I have a button that the user presses and sends a grid to Excel, and it's saving the file in a directory that I don't want the user to have access to. (It's a hidden share) It then opens the file immediately for the user to view.

I'm using the Microsoft.Office.Interop.Excel namespace, so I'm using an Excel Application and Workbook.

I want the application to open the file from the directory, but if the user chooses to save the file using SaveAs (I would prefer that if the user clicked Save, that it treated it as a new file and opened the SaveAs Dialog), I would like the initial directory to be something like C:.

Here is a bit of the relevant code:

Public Sub sendToExcel()

        'Code here that exports data to Excel

        Dim xlApp As Excel.Application = New Excel.Application()
        Dim xlWorkbook As Excel.Workbook = xlApp.Workbooks.Open(filePath)

        xlApp.Visible = True

End Sub

The most obvious thing I've tried is this:

xlApp.DefaultFilePath = "C:\\"

This seems to only work if the file is new (hasn't been saved).

Any suggestions?

Does the file have to be saved in that hidden share?

If not, you could save it in the user's temporary folder using the Path.GetTempFileName() and open it with Excel from there.

That way, when they "Save As", they will only see the local temporary folder as the source.

Of course, this works only if it is a desktop application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM