none
activate a worksheet by index RRS feed

  • Question

  • I have a winform with a forward and a backwards button. What I am trying to do is to allow the user to move back and forth on the workbook by clicking on the button. I thought that the best way to achieve this is by using index. I worked out the code below, but because I have Option Strict On, it tells me that it cannot allow late binding.

    the error is here:

    WB.WorkSheets(WS.Index -1).Activate()

    I am not really sure how I can change my code.

    here it is:

        Option Explicit On
    Option Strict On
    
    'Import Libraries
    Imports Microsoft.Office.Tools.Excel
    Imports System.Drawing
    Imports System
    Imports System.IO
    Imports System.Drawing.Printing
    Imports System.Windows.Forms
    
    Public Class frmNavigation
    
        Dim WB As Excel.Workbook
        Dim WS As Excel.Worksheet
    
    
        Private Sub btnMoveBack_Click(sender As Object, e As EventArgs) Handles btnMoveBack.Click
    
            'This event is triggered when the Previous Sheet button is
            'clicked. The sheet moves to the previous sheet. This event
            'is only run throughout the clientworksheets as the tabs and other
            'standard excel navigation may be disabled.
    
            WB = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)
            WS = CType(WB.ActiveSheet, Excel.Worksheet)
    
            WS.Application.ScreenUpdating = False
    
    
    
           WB.WorkSheets(WS.Index -1).Activate()()                              
    
            WS.Application.ScreenUpdating = True
    
    
        End Sub

    Wednesday, July 17, 2013 4:06 AM

Answers

  • VB not allow late binding if you set "Option Strict On".

    You need to Annotation the second line "Option Strict On" to allow late binding.

    I have tried to copy your code and run.

    After I changed,it is successful.

    Wednesday, July 17, 2013 3:47 PM

All replies

  • Which line of code is actually triggering the error and what is the exact error message?

    Are you trying to work with a workbook other than the VSTO workbook to which this code is attached?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 17, 2013 1:32 PM
    Moderator
  • VB not allow late binding if you set "Option Strict On".

    You need to Annotation the second line "Option Strict On" to allow late binding.

    I have tried to copy your code and run.

    After I changed,it is successful.

    Wednesday, July 17, 2013 3:47 PM