Search code examples
vb.netsavefiledialog

STAThreadAttribute with SaveFileDialog VB.NET


I have an application to export ListView to Excel sheet , and im trying to do this in background but i have error in SaveFileDialog.showdialog().This is the error :

An exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

and this is my code :

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    BackgroundWorker1.RunWorkerAsync()
End Sub

Public Sub saveExcelFile(ByVal FileName As String)
    Try
        Dim xls As New Excel.Application
        Dim sheet As Excel.Worksheet
        Dim i As Integer
        xls.Workbooks.Add()
        sheet = xls.ActiveWorkbook.ActiveSheet
        Dim row As Integer = 1
        Dim col As Integer = 1
        For i = 0 To Me.ListView1.Columns.Count - 1
            sheet.Cells(1, i + 1) = Me.ListView1.Columns(i).Text
        Next
        For i = 0 To Me.ListView1.Items.Count - 1
            For j = 0 To Me.ListView1.Items(i).SubItems.Count - 1
                sheet.Cells(i + 2, j + 1) = Me.ListView1.Items(i).SubItems(j).Text
            Next
        Next

        row += 1
        col = 1

        ' for the header 
        sheet.Rows(1).Font.Name = "Cooper Black"
        sheet.Rows(1).Font.size = 12
        sheet.Rows(1).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
        Dim mycol As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#148cf7")
        sheet.Rows(1).Font.color = mycol
        ' for all the sheet without header
        sheet.Range("a2", "z1000").Font.Name = "Arial"
        sheet.Range("a2", "z1000").Font.Size = 13
        sheet.Range("a2", "z1000").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter

        sheet.Range("A1:X1").EntireColumn.AutoFit()
        sheet.Range("A1:X1").EntireRow.AutoFit()

        xls.ActiveWorkbook.SaveAs(FileName)
        xls.Workbooks.Close()
        xls.Quit()
    Catch ex As Exception

    End Try
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
     Try

        Dim saveFileDialog1 As New SaveFileDialog
        saveFileDialog1.Filter = "Excel File|*.xlsx"
        saveFileDialog1.Title = "Save an Excel File"
        saveFileDialog1.ShowDialog()
        If saveFileDialog1.FileName <> "" Then
            saveExcelFile(saveFileDialog1.FileName)
        End If

    Catch ex As Exception

    End Try
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    MsgBox("done")
End Sub

Solution

  • i put this code in load form and everything is perfect:

    Control.CheckForIllegalCrossThreadCalls = False