I have created an application that uses Microsoft.Office.Interop for Excel.

I'm trying to implement both an OpenFileDialog and a SaveFileDialog. I got the OpenFileDialog to work, but I have run into a wall on the SaveFileDialog. Because when I get to the SaveFileDialog box, it lets me pick the location, file name and extension...but nothing shows up in the spot where I'm saving to. I feel like I'm missing something simple, so maybe someone in here can help.

The code I have so far (not sure where it's even correct or not) is below. What I am prompting the SaveFileDialog for is to save an Excel file. How do I link the Excel object to the path created by the SaveFileDialog?

public void Save_File()
      {
         //creating an SaveFileDialog object, to save Excel File.
         SaveFileDialog dialog = new SaveFileDialog();
         dialog.InitialDirectory = "c:/desktop";
         dialog.Title = "Select Where To Save File";
         dialog.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*";
         dialog.ShowDialog();
         
      }//end method Save_File

Thanks in advance!

Recommended Answers

All 6 Replies

public void Save_File()
    {
    //creating an SaveFileDialog object, to save Excel File.
    SaveFileDialog dialog = new SaveFileDialog();
    dialog.InitialDirectory = "c:/desktop";
    dialog.Title = "Select Where To Save File";
    dialog.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*";
    if (dialog.ShowDialog() == DialogResult.Cancel)
    return;
    // now you have to save the the file which you were missing 
    }//end method Save_File

Thanks for the reply.

This is my first project using the Excel Automation tools. And I'm fairly new to programming in general.

Shouldn't "DialogResult.Cancel" be "DialogResult.OK"?

And also can't figure out what code to put in that if statement that will actually save what needs to be saved. What method do I need to be calling?

Thanks again!

Nevermind. I just figured it out :)

Hi, you can check it here.
And btw, if you figure it out, can you please share your working code with us?
thx in advance.

I didn't get to post the updated code that I got to work before leaving work, and don't have it on my computer here, but I'm pretty sure all I had to do was add one line to what Chico suggested...just changed "Cancel" to "Ok".

I want to say that inside the if statement I had to use the SaveAs method of the SaveFileDialog, with a parameter of the FileName.

Once I get back to work on Monday, I'll update this post.

I didn't get to post the updated code that I got to work before leaving work, and don't have it on my computer here, but I'm pretty sure all I had to do was add one line to what Chico suggested...just changed "Cancel" to "Ok".

I want to say that inside the if statement I had to use the SaveAs method of the SaveFileDialog, with a parameter of the FileName.

Once I get back to work on Monday, I'll update this post.

Using if (dlgReult == Cancel)
return;

Above not exact code but all that is saying is if the dialog result is equal to dialogreult.cancel then return back to where you were else do next action just another way to do the same thing :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.