Solved

Excel VB.net Add in Strange Error

Posted on 2014-07-25
6
604 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:murbro
  • 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:murbro
ID: 40222713
It happens at the same line in the loop
0
 

Author Comment

by:murbro
ID: 40222716
I took the line "ImgStream.Dispose() " and it still bombed. I use a fresh spreadsheet each time
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Accepted Solution

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

Did you try commenting out imgstream.dispose?
0
 

Author Closing Comment

by:murbro
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now