none
Declaring Global Variable for Excel Application RRS feed

  • Question

  • Hi friends,

    In what manner may the Excel Application be declared in an AddIn so as to be referred to anywhere in the entire solution?

    In other words I have to usually declare the same as:

    Public XLApp As Excel.Application    'Declared at the top of every module after the Public Class line

    and then

    XLApp = Globals.ThisAddIn.Application    'To assign the AddIn's Application in each and every procedure

    Any way to declare the same and assign the said value once only and be used anywhere in the entire solution instead of doing so at every Module and every Procedure level respectively?

    Looking forward for your help.

    Thanx in advance.


    Best Regards, Faraz A Qureshi

    Sunday, January 5, 2014 3:58 PM

Answers

  • Hi Faraz,

    According to your description, you want to declare a global variable for Excel application and use it in the entire solution. Does solution means the project?
    If I understand correctly, you can add a module to the project and initialize the variable in the startup event like code below:

    Module Module1
        Public excelApp As Excel.Application
    End Module
    
    Private Sub ThisAddIn_Startup() Handles Me.Startup
        Module1.excelApp = Globals.ThisAddIn.Application
    End Sub
    

    Then you can use the Module1.excelApp as Excel Application in the entire project like below:

      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            MsgBox(Module1.excelApp.ActiveCell.Value)
        End Sub
    

    Best regards

    Fei


    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.

    Monday, January 6, 2014 9:44 AM
    Moderator