0

I am trying to open an existing file in WorkBookView (named wkbMain in code given below) placed on my Windows Form. I am using the following code:

Private Sub MenuItemOpen_Click(sender As Object, e As EventArgs)
    Dim lObjDialog As New OpenFileDialog

    wkbMain.GetLock()

    Try
        If lObjDialog.ShowDialog() = DialogResult.OK Then
            wkbMain = SpreadsheetGear.Factory.GetWorkbook(lObjDialog.FileName, System.Globalization.CultureInfo.CurrentCulture)
        End If
    Catch ex As Exception

    Finally
        wkbMain.ReleaseLock()
    End Try
End Sub

But the assignment

wkbMain = SpreadsheetGear.Factory.GetWorkbook(lObjDialog.FileName, System.Globalization.CultureInfo.CurrentCulture)`) 

throws an exception:

Unable to cast object of type 'ᢷ' to type 'SpreadsheetGear.Windows.Forms.WorkbookView'.

Please suggest a solution

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ammar
  • 152
  • 2
  • 12

1 Answers1

1

You need to set the WorkbookView.ActiveWorkbook property to the object returned by Factory.GetWorkbook(...), not on your WorkbookView object itself. Example:

wkbMain.ActiveWorkbook = SpreadsheetGear.Factory.GetWorkbook(lObjDialog.FileName, 
    System.Globalization.CultureInfo.CurrentCulture)) 
Tim Andersen
  • 3,014
  • 1
  • 15
  • 11
  • Thanks a lot @Tim Andersen. Looks like you are a good helping hand for me in Spreadsheetgear programming. Also I am going to finish my tasks with your kind help. Once again thanks a lot for your kind help. – Ammar Oct 29 '14 at 04:45