Need help making Excel visible. RRS feed

  • Question

  • I am trying to activate a work sheet in excel from Visual Basic using the user interface.  I get the workbook using GetObject and the work sheet is in the file.  However, it is not visible.  How can I fix this? Here is my code.    




    Dimexcelbj AsObject

    DimoSheet AsWorksheet


    ' Show Excel through its Application property.




    DimfileName AsString= "C:\Users\HCT_Files\Desktop\Excel default file.xlsx"


    DimwinCount AsInteger


    IfNotMy.Computer.FileSystem.FileExists(fileName) Then

                MsgBox(fileName &

    " C:\Users\HCT_Files\Desktop\Excel default file.xlsx")






    ' Set the object variable to refer to the file you want to use.


    'Dim fn_sheet As String = String.Concat(fileName, "!Sheet1")

            excelbj = GetObject(fileName)


    ' Show Excel through its Application property.

            excelbj.Application.Visible =



    ' Get access to the worksheet

            oSheet =

    CType(excelbj.ActiveSheet, Worksheet)


    ' Show the window containing the file.


    Dimindex AsInteger


    Dimerr_count AsInteger= 0


    Forindex = 1 To10



                    winCount = excelbj.Parent.Windows.Count()

                    excelbj.Parent.Windows(winCount).Visible =



    ' Succeeded so break out of loop




    Catchex AsException

    err_count = err_count + 1


    'MsgBox("Attempt to access Excel object failed - please close any apps that may be locking it.")


    'winCount = excelbj.Parent.Windows.Count()


    'xcelbj.Parent.Windows(winCount).Visible = True







    End Class

    <input id="740370d5-c932-436e-87ab-e9d175a5074d_attachments" type="hidden" value="" />                
    Avatar of Theresa 2007                       
    Theresa 2007
    Theresa 2007        
             Joined  Jul 2016        
    0 Points               
    Monday, August 1, 2016 4:58 PM


  • Hi Theresa 2007,

    From the code you had posted above I can assume that you are trying to run the Demo example provided on MSDN Site for Get Object.

    Following is the original code.

    Private Sub getExcel()
        Dim fileName As String = "c:\vb\test.xls" 
        If Not My.Computer.FileSystem.FileExists(fileName) Then
            MsgBox(fileName & " does not exist")
            Exit Sub 
        End If 
        ' Set the object variable to refer to the file you want to use. 
        Dim excelObj As Object = GetObject(fileName)
        ' Show Excel through its Application property. 
        excelObj.Application.Visible = True 
        ' Show the window containing the file. 
        Dim winCount As Integer = excelObj.Parent.Windows.Count()
        excelObj.Parent.Windows(winCount).Visible = True 
        ' Insert additional code to manipulate the test.xls file here. 
        ' ...
        excelObj = Nothing 
    End Sub

    GetObject Function (Visual Basic)

    I try to test the code and find that when you try to run the code and if Excel Application is not running at that time you will get error.

    because at that time value of wincount=0.

    so to run this code successfully you need to run the Excel Application before run this code.

    after it will run correctly.

    or if you don't want to do it then you need to check the value of wincount in the code.

    if it is 0 then you need to Exit the code.



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 2, 2016 2:18 AM