0

I have a Windows Forms app with an Excel file I created using SpreadsheetGear. My application uses SaveFileDialog() to prompt the user to save the file to their computer using the following code:

'Bring up the save dialog to save the newly created Excel file
Using saveFileDialog1 As New SaveFileDialog()
    saveFileDialog1.FileName = "ExportFile.xlsx"
    If DialogResult.OK <> saveFileDialog1.ShowDialog() Then
        Return
    End If
    Try
        e.Result.SaveAs(saveFileDialog1.FileName, FileFormat.OpenXMLWorkbook)
    Catch ex As Exception
        MessageBox.Show("Error: " & ex.Message, "Save Error", MessageBoxButtons.OK)
    End Try
End Using

That code works fine but the user doesn't want to worry about saving the file. He just wants it to open right away so he can copy/paste the information he needs and then discard the file without having to navigate to his save destination. I can't seem to get SaveFileDialog() to do that for me.

Is there a better way to do this?

braX
  • 11,506
  • 5
  • 20
  • 33
TheIronCheek
  • 1,077
  • 2
  • 20
  • 50
  • Which part isn't working, though? – rlb.usa Mar 27 '18 at 16:10
  • @rlb.usa - The code above works, it just doesn't open the file. I need to be able to open the file automatically after saving, or skip the `SaveFileDialog()` altogether and open it without saving. – TheIronCheek Mar 27 '18 at 16:13
  • does this question help you? https://stackoverflow.com/questions/18246053/how-can-i-create-a-link-to-a-local-file-on-a-locally-run-web-page I know that you want the browser to open the local file but I'm really pretty sure that isn't possible (think of the malicious applications to that!) and that the best you can do is produce a link on the screen saying to "click here" and that opens the `file:///` – rlb.usa Mar 27 '18 at 16:17
  • 1
    @rlb.usa - This is a Windows Forms app, not website so that question doesn't help much. – TheIronCheek Mar 27 '18 at 16:20
  • My apologies! How about [1](https://stackoverflow.com/questions/23685260/open-a-file-without-dialog), [2](https://stackoverflow.com/questions/1263077/opening-and-saving-file-without-save-open-dialogue?rq=1) – rlb.usa Mar 27 '18 at 16:26
  • @rlb.usa - This one got me where I needed to go: https://stackoverflow.com/questions/17362716/opening-an-excel-file-from-button-click-on-a-windows-form – TheIronCheek Mar 27 '18 at 18:08

1 Answers1

0

This closed question helped me discover my answer. Process.Start with the filename from the SaveFileDialog works.

Updated Code:

'Bring up the save dialog to save the newly created Excel file
Using saveFileDialog1 As New SaveFileDialog()
    saveFileDialog1.FileName = "ExportFile.xlsx"
    If DialogResult.OK <> saveFileDialog1.ShowDialog() Then
        Return
    End If
    Try
        e.Result.SaveAs(saveFileDialog1.FileName, FileFormat.OpenXMLWorkbook)

        If File.Exists(saveFileDialog1.FileName) Then
                Process.Start(saveFileDialog1.FileName) 'Open the newly created Excel file
        Else
            Throw New Exception("Could not find file to open. Please try again.")
        End If
    Catch ex As Exception
        MessageBox.Show("Error: " & ex.Message, "Save Error", MessageBoxButtons.OK)
    End Try
End Using
TheIronCheek
  • 1,077
  • 2
  • 20
  • 50