Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6203397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:54:33+00:00 2026-05-24T04:54:33+00:00

I have set up a timer in an Office addin (being developed in VB.net)

  • 0

I have set up a timer in an Office addin (being developed in VB.net) that I can set going fine, using the code:

Public Class ThisAddIn

 Friend WithEvents Timer1 As System.Timers.Timer

 Private Sub ThisAddIn_Startup() Handles Me.Startup

    Me.Timer1 = New System.Timers.Timer()
    Me.Timer1.Interval = 500
    Me.Timer1.Enabled = True

 End Sub

 Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

    Me.Timer1.Enabled = False
    MsgBox("Code Ran!")

 End Sub

I would like to call this timer when a user changes a setting in the addin’s ribbon, but I cannot seem to access the event. If I use the code:

Public Class ServerRibbon

 Public myCaller As ThisAddIn

 private sub respondToClick()

    Dim blah As System.Timers.Timer
    blah = myCaller.Timer1
    blah.Enabled = True

 end sub

I get an error of ‘object reference not set to an instance of an object’. Can anyone explain how I can set the timer going?

Thanks in advance,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:54:35+00:00Added an answer on May 24, 2026 at 4:54 am

    Your code doesn’t work because myCaller is not set to your particular instance of ThisAddIn (theoretically, there could be many ThisAddIn instances floating around your memory).

    However, since you are developing an office add-in, there is in fact only a single instance of ThisAddIn. A simple workaround to get access to the timer would be to make Timer1 a shared variable:

    Friend Shared WithEvents Timer1 As System.Timers.Timer
    

    This would allow you to access the timer as ThisAddIn.Timer1.

    (Note that, in general, publicly accessible shared fields (aka global variables) are a bad idea. However, in your case, it might just be the simplest solution that allows you to do what you want.)

    PS: You might want to read up on the difference between a class and an instance of a class.


    EDIT: Since you have an event handler attached to the timer, you either

    (a) need to make the event handler shared as well (Private Shared Sub Timer1_Elapsed...), which has the disadvantage that your event handler cannot access instance variables of ThisAddIn anymore, or

    (b) Make the instance of ThisAddIn accessible through a separate property instead of making the field shared:

    Public Class ThisAddIn
    
        Friend WithEvents Timer1 As System.Timers.Timer
    
        Private Shared _instance As ThisAddIn
        Public ReadOnly Shared Property Instance() As ThisAddIn
            Get
                Return _instance
            End Get
        End Property
    
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            _instance = Me
    
            Me.Timer1 = New System.Timers.Timer()
            Me.Timer1.Interval = 500
            Me.Timer1.Enabled = True
         End Sub
    
    ...
    
    Public Class ServerRibbon
        Private Sub respondToClick()
            ThisAddIn.Instance.Timer1.Enabled = True
        End Sub
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I have SQL repeat some set-based operation an arbitrary number of times
I have set up a version control system using TortoiseSVN at my home to
I have set up a Django application that uses images. I think I have
I have set of scripts for doing scripted installs. You can use the scripts
I have created an office scheduling program that uses jQuery to post to a
I have config.time_zone in environment.rb set to UTC, and my mySQL server returns the
We have set up a system where notifications get sent to a user with
I have set a canvas' background to an image of a company logo. I
I have set up transactional replication between two SQL Servers on different ends of
I have set the FlushMode property on an NHibernate session to FlushMode.Never, but when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.