Try
Dim projectDirectoryName As String = GetProjectDirectoryName()
Dim projectDirectoryPath As String = GetProjectDirectoryPath()
saveDialog.FileName = projectDirectoryName & " - Replaced Components " & strFileNamePrefix & " to " & OverridedPartNamePrefix & ".xlsx"
saveDialog.Filter = "Excel Workbook|*.xlsx"
saveDialog.Title = "Save Excel File"
saveDialog.InitialDirectory = projectDirectoryPath
Dim DialogResult As DialogResult = saveDialog.ShowDialog()
If DialogResult = DialogResult.OK Then
If Not String.IsNullOrEmpty(saveDialog.FileName) Then
workbook.SaveAs(saveDialog.FileName)
MessageBox.Show("Export successful!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Export cancelled or encountered an error.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Catch ex As Exception
MessageBox.Show("An error occurred while exporting to Excel: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If workbook IsNot Nothing Then workbook.Close(False)
If excelApp IsNot Nothing Then excelApp.Quit()
ReleaseObject(worksheet)
ReleaseObject(workbook)
ReleaseObject(excelApp)
End Try
This snippet is the one that I used in order to export custom-created list to excel. Hope it will help. Basicaly;
saveDialog.FileName = projectDirectoryName & " - Replaced Components " & strFileNamePrefix & " to " & OverridedPartNamePrefix & ".xlsx"
saveDialog.Filter = "Excel Workbook|*.xlsx" 'this indicates save as file type
saveDialog.Title = "Save Excel File" 'this indicates save as window title name
saveDialog.InitialDirectory = projectDirectoryPath 'this indicates save path
This part of code does all the work. Hope it will help !