I want to create an Excel spreadsheet from a .NET Application and then set the Excel.Application instance current directory to a custom folder. I want to do this so when the user clicks the save button in Excel, the Save as dialog is already located in the correct directory.
I know it's possible to change the current directory in Excel Instance with VBA.FileSystem.ChDir and doing it within the Excel.Application instance with VBA Code/Macro like this:
Create an Excel Spreadsheet from C# .NET
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Workbooks.Add(); excel.Visible = true;
When Excel spreadsheet is open, hit ALT + F11. Then create and run following macro
Sub SetChdir() Call FileSystem.ChDir("C:\To\My\Custom\Directory\") End Sub
When clicking Save in Excel the Current Directory is set path set with ChDir
I can't figure out, or even if it's possible, how to create this behaviour from .NET. I tired messing around with Excel.DefaultFilePath which Excel uses on startup to set the Current Directory. But it has two problems:
- When DefaultFilePath is set to a different value, you need to restart Excel to make it effect on new or open spreadsheets
- I really don't want to change the users current DefaultFilePath as it is a Global value for Excel and not this individual spreadsheet.