0

I have a project where in my program opens a excel file called Template.xlsx and makes some updates in it and then when i run this line of code

xlWorkBook.Close(true, Type.Missing, Type.Missing);

I get the save as dialog box which is great but the problem is in the name field i get "Copy of Template.xlsx". I dont want that. I want to fill that field with for example "Order25.xlsx". is it possible?

Saeed Asgari
  • 357
  • 2
  • 14
  • ...you're asking how to save a workbook with a new name using VBA, is that right? – ashleedawg Jan 10 '18 at 09:47
  • well its in c# my app opens a excel file which is the users template. and fills in the info and then when its done it closes the workbook using that line of code. i want to show the saving dialog box before closing and fill in custom name for the saving file. – Saeed Asgari Jan 10 '18 at 09:50
  • You could try `xlWorkBook.SaveAs(*path*)`, although it would require you to know the path you're saving to, e.g. `xlWorkBook.SaveAs(@"C:\temp\Order25.xlsx")` – Keyur PATEL Jan 10 '18 at 09:51
  • 1
    what about this: https://stackoverflow.com/a/21199578/8112776 – ashleedawg Jan 10 '18 at 09:52
  • @SaeedAsgari, You can make it manually , but if you think it should make based on last book name in DB , you should last name and parse it's name and increase that number. – Aria Jan 10 '18 at 09:56
  • @Keyur in that case the path is already known and the dialog box would never be shown. its required that the user click on save by themselves. so we only want to set the filename but not the path. – Saeed Asgari Jan 10 '18 at 10:00
  • @ashleedawg thats great thanks – Saeed Asgari Jan 10 '18 at 10:23

1 Answers1

1

Based on the suggestions in the comments, here is a possible solution using SaveFileDialog (you'll need using System.Windows.Forms):

SaveFileDialog saveFileDialog1 = new SaveFileDialog();  
saveFileDialog1.Filter = "Excel|*.xls|Excel 2010|*.xlsx";;  
saveFileDialog1.Title = "Save the Excel File";
saveFileDialog1.FileName = "Order25.xlsx";

if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    xlWorkBook.SaveAs(saveFileDialog1.FileName);
} 

I haven't tested it yet since I'm not near a computer, so let me know if it doesn't work.

Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41
  • Yeah thats what i did. came back to right it down as answer but you already made an answer. so i mark it as solved. Thanks. – Saeed Asgari Jan 10 '18 at 10:24