Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VB.net Add in Strange Error

Posted on 2014-07-25
6
Medium Priority
?
703 Views
Last Modified: 2014-07-30
Hi

I get the following error that occurs looping through a DataGridView that has an image column and adding the data to Excel. I have no idea what this is

Transition into COM context 0x564070 for this RuntimeCallableWrapper failed with the following error: System call failed. (Exception from HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED)). This is typically because the COM context 0x564070 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else. Releasing the interfaces from the current COM context (COM context 0x564298). This may cause corruption

    Public Sub Send_to_Excel(ByVal oAnchorRangeAddress As String)
        Dim oPGB As ProgressBar = Me.ProgressBar1

        Try

            Dim D As DataGridView = Me.DataGridView1

            oPGB.Minimum = 1
            oPGB.Maximum = D.RowCount

            Dim oResponse As MsgBoxResult
            If D.RowCount > 1000 Then
                oResponse = MsgBox("You are about to load " & CStr(D.RowCount) & " lines. Would you like to continue?", MsgBoxStyle.YesNo)
                If oResponse = MsgBoxResult.No Then
                    Exit Sub
                End If
            End If

            oPGB.Visible = True

            Globals.ThisAddIn.Application.Cells.RowHeight = 120
            Globals.ThisAddIn.Application.Range("1:1").ColumnWidth = 150

            Dim rowCounter As Long

            Dim c As Integer
            Dim r As Long

            'Populate column headers first
            For c = 0 To D.ColumnCount - 1
                Globals.ThisAddIn.Application.Cells(1, c + 1).value = D.Columns(c).Name
            Next

            For r = 0 To D.RowCount - 1 'Take care of rows and progess bar that relies on its count

                rowCounter = rowCounter + 1
                oPGB.Value = rowCounter
                ' Refresh the Progress Bar
                oPGB.Refresh()
                For c = 0 To D.ColumnCount - 1 'take care of columns for each row
                    Try

                    If c = 0 Then

                        If D.Rows(r).Cells(2).Value.ToString = "Yes" Then
                            Dim oID As String = D.Rows(r).Cells(1).Value
                            Call oPositionPicture(oID, r + 2, c + 1)
                        End If

                    Else
                        Globals.ThisAddIn.Application.Cells(r + 2, c).value = D.Rows(r).Cells(c).Value
                    End If
                    Catch ex As Exception
                        MsgBox("Loop Error" & ex.Message)
                    End Try
                Next
            Next

            'Globals.ThisAddIn.Application.Columns.AutoFit()


            oPGB.Visible = False

        Catch ex As Exception
            MsgBox(ex.Message & " a155")

            oPGB.Visible = False
        End Try
    End Sub

    Sub oPositionPicture(ByVal oID As String, ByVal oExcelRow As Long, ByVal oExcelCol As Integer)

        'If Me.DataGridView1.NewRowIndex < 1 Then
        'MsgBox("Please select a row in the grid")
        'Exit Sub
        'End If

        'Dim sSQL As String = "Select Photo from Photos where ID = '" & _
        'DataGridView1.CurrentRow.Cells(13).Value() & "'"

        Dim sSQL As String = "Select Photo from Photos where ID = " & oID & ""

        Dim con As New SqlConnection(My.Settings.CS)
        Dim cmd As New SqlCommand(sSQL, con)

        Try

            con.Open()

            Dim ImgStream As New IO.MemoryStream(CType(cmd.ExecuteScalar, Byte()))
            PictureBox1.Image = System.Drawing.Image.FromStream(ImgStream)
            Clipboard.Clear()
            Clipboard.SetData(DataFormats.Bitmap, Me.PictureBox1.Image)
            Globals.ThisAddIn.Application.Cells(oExcelRow, oExcelCol).select()
            Globals.ThisAddIn.Application.ActiveSheet.Paste()

            ImgStream.Dispose()

            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Open in new window

0
Comment
Question by:Murray Brown
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:FamousMortimer
ID: 40219495
When does the error occur?  Does it always occur at the same line at the same loop?  What happens with another spreadsheet?

Just a hunch but I am wondering if this is causing it...

ImgStream.Dispose()
0
 

Author Comment

by:Murray Brown
ID: 40222713
It happens at the same line in the loop
0
 

Author Comment

by:Murray Brown
ID: 40222716
I took the line "ImgStream.Dispose() " and it still bombed. I use a fresh spreadsheet each time
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Accepted Solution

by:
FamousMortimer earned 2000 total points
ID: 40222717
Ok... which line?

Did you try commenting out imgstream.dispose?
0
 

Author Closing Comment

by:Murray Brown
ID: 40229562
Hi, that helped. Thanks for the help
0
 
LVL 10

Expert Comment

by:FamousMortimer
ID: 40229596
Glad you got it working.

What was the problem exactly?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question