Reputation: 99
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..
Upvotes: 3
Views: 12152
Reputation: 497
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
Upvotes: 1
Reputation: 1
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
Upvotes: 0
Reputation: 9024
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
Upvotes: 3
Reputation: 3863
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
Upvotes: 0