Getting several Errors when adding ActiveCell RRS feed

  • Question

  • Hey,

    I used help and got the following code to add super and subscript connectors for excel but I am getting the following errors:


    Error 1 Reference to a non-shared member requires an object reference.

    Error 2 'SelLength' is not a member of 'System.Windows.Forms.TextBox'.

    Error 3 'SelStart' is not a member of 'System.Windows.Forms.TextBox'.

    Here is my code for the control:

    Imports Microsoft.Office.Interop.Excel
    Public Class UserControl1
        Private Sub btnSuper_Click(sender As Object, e As EventArgs) Handles btnSuper.Click
            Dim intStart As Integer
            Dim intLength As Integer
            intLength = TextBox1.SelLength
            If intLength > 0 Then
                intStart = TextBox1.SelStart + 1
                Application.ActiveCell.Characters(intStart, intLength).Font.Superscript = True
            End If
        End Sub
        Public Sub New()
            ' This call is required by the designer.
            ' Add any initialization after the InitializeComponent() call.
        End Sub
        Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles Me.Load
            TextBox1.Text = Application.ActiveCell.Formula
        End Sub
    End Class

    Here is my code for the AddIn Launch

    Imports Microsoft.Office.Interop.Excel
    Public Class ThisAddIn
        Private Sub ThisAddIn_Startup() Handles Me.Startup
        End Sub
        Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
        End Sub
    End Class

    The error is coming where ever I reference ActiveCell

    Tuesday, February 4, 2014 9:26 AM


  • Hi,

    Based on the code you provide, you are developing an Excel Add-in with VB.Net.

    First of all, we should define a new instance of Excel application object before using it. In an application-level add-in, we could assign it with “Globals.ThisAddIn.Application”.

    Dim app As Excel.Application = Globals.ThisAddIn.Application
    app.ActiveCell.Characters(intStart, intLength).Font.Superscript = True

    Then, SelLength and SelStart are properties of Textbox in VB6.0. In VB.Net, they are SelectionLength and SelectionStart.

    In addition, I find you want to show this user control when the add-in Startup and you use the code below to show the user control.


    In fact, the user control will not show in Excel application in this way since it is a control rather than a form or task pane. To show a user control, we could add it into a Custom Task Pane or a Windows Form and then show the pane or form. Here is a sample to show a user control in a Custom Task Pane.

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        Dim myUserControl1 As UserControl1 = New UserControl1
        Dim myCustomTaskPane As Microsoft.Office.Tools.CustomTaskPane
        myCustomTaskPane = CustomTaskPanes.Add(myUserControl1, "New Task Pane")
        myCustomTaskPane.Visible = True
    End Sub

    By the way, if you show the user control in the task pane when the add-in startups, the TextBox1.Text = Application.ActiveCell.Formula will throw an exception since there is no ActiveCell object when loading an add-in. So I don’t suggest you setting the text of TextBox1 with ActiveCell.Formula when loading the add-in. You could add a button in the ribbon to show the user control in a task pane after loading the workbook.

    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.

    Wednesday, February 5, 2014 7:04 AM