3

Iam using visual studio 2012,

i would like to open "save dialog" to choose where to save my file instead of using fixed path, the following code is a sample of what i would like to use it in:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim xls As New Microsoft.Office.Interop.Excel.Application

    Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\Resources\")
    Dim fileName = "book1.xlsx"

    xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
    xlsWorkSheet = xlsWorkBook.Sheets("a")

    xlsWorkSheet.Cells(1, 1) = TextBox1.Text

    xlsWorkBook.SaveAs("C:\output\book1.xlsx")

    xlsWorkBook.Close()
    xls.Quit()


End Sub

i would like to change this path "C:\output\book1.xlsx" to save dialog, so i can choose where to save it manually.

thanks alot..

4 Answers 4

3

Like this, don't forget to dispose of com objects with the Marshal class like I added.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
 Dim xls As New Microsoft.Office.Interop.Excel.Application
 Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
 Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
 Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\Resources\")
 Dim fileName = "book1.xlsx"
 xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
 xlsWorkSheet = xlsWorkBook.Sheets("a")
 xlsWorkSheet.Cells(1, 1) = TextBox1.Text
 Using sfd As New SaveFileDialog
  If sfd.ShowDialog() = DialogResult.OK Then
   xlsWorkBook.SaveAs(sfd.FileName)
   MessageBox.Show(sfd.Filename)
  End If
 End Using
 xlsWorkBook.Close()
 xls.Quit()
 Marshal.FinalReleaseComObject(xlsWorkSheet)
 Marshal.FinalReleaseComObject(xlsWorkBook)
 Marshal.FinalReleaseComObject(xls)
End Sub
Sign up to request clarification or add additional context in comments.

12 Comments

thanks alot, it's working .. I added the " Using sfd As New SaveFileDialog sfd.ShowDialog() xlsWorkBook.SaveAs(sfd.FileName) End Using " part only. please if you can clarify the marshal part, because i don't get it., also another simple thing, is there a way i can show a message box with the location i chose to save the file to.., thanks alot for your time
., any suggestion regarding the message box part (show a message box with the location i chose to save the file to)., thanks
If you don't use the Marshal.FinalReleaseComObject you can find your excel still open as a process in the Task Manager.
., thanks for your clarification, i got your point & read about it & understand it totally..thanks, but the problem is it works with me only if i close the application first, but if i kept my application running the excel process is still there, so it will be great if you any method to get rid of this.
Are you holding any instances of an Excel object anywhere - like class level?
|
1

A little more comprehensive way to open the Save As Dialog than OneFineDay's answer (using the same method).
This opens the Save As dialog using a designated directory, filename, extension type, and window title; it also prompts before overwritting any existing files.

Dim dir as String = "C:\output\"
Dim fName As String = "Book1"

Using sfd As New SaveFileDialog
    sfd.InitialDirectory = dir
    sfd.Title = "Save As"
    sfd.OverwritePrompt = True
    sfd.FileName = fName
    sfd.DefaultExt = ".xlsx"
    sfd.Filter = "Excel Workbook(*.xlsx)|"
    sfd.AddExtension = True
    If sfd.ShowDialog() = DialogResult.OK Then
        xlsWorkBook.SaveAs(sfd.FileName)
    End If
End Using

Comments

0

Add an openfiledialog to your form and then ...

  With OpenFileDialog1
        .Title = " whatever"
        .InitialDirectory = "c:\"
        .Multiselect = False
        If .ShowDialog() = DialogResult.OK Then
            xlsWorkBook.SaveAs(.FileName)
        End If

End With

Comments

0

You can use this :

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim xls As New Microsoft.Office.Interop.Excel.Application

    Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\Resources\")
    Dim fileName = "book1.xlsx"

    xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
    xlsWorkSheet = xlsWorkBook.Sheets("a")

    xlsWorkSheet.Cells(1, 1) = TextBox1.Text

    xlsWorkBook.SaveAs("C:\output\book1.xlsx")
    xls.Application.DisplayAlerts = False
    xlsWorkBook.Close()
    xls.Quit()


End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.