none
Need help with Getobject for excel opening a worksheet in my workbook using Visual Basic?

    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.    

    Public

    ClassgetExcel

      

    Dimexcelbj AsObject

    DimoSheet AsWorksheet

       

    ' Show Excel through its Application property.


       

    PublicSubgetExcel()

           

    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")

               

    ExitSub


           

    EndIf


           

    ' 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 =

    True


           

    ' 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

               

    Try


                    winCount = excelbj.Parent.Windows.Count()

                    excelbj.Parent.Windows(winCount).Visible =

    True


                   

    ' Succeeded so break out of loop


                   

    ExitFor


               

    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


               

    EndTry

          

    Next

     

    EndSub

    End Class

    Tuesday, July 26, 2016 7:52 PM

Answers