Results 1 to 6 of 6

Thread: passing a excel sheet object from one form to another

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    91

    passing a excel sheet object from one form to another

    Hi,

    I have 2 forms lets assume its A and B. I want to pass a excel sheet object from form A to B so i can run a macro in the excel sheet from the form B.

    How can i pass a object from one form to another.

    thanks

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    You may just declare your excel object as Public at the module level and use it throughout your app lifecycle.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    91
    I am a newbie I am lost. Could u please give me an example

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    91
    Ok I kinda understand and did this in module level,
    Code:
    Public objXL As Excel.Application
    Public objWB As Excel.Workbook
    Public objWS As Excel.Worksheet
    Public objMacro As Variant


    and now i set the objects in form A,
    Code:
    Set objXL = New Excel.Application
    Set objWB = objXL.Workbooks.Open(FileName:=path + "TimesExcel.xls")
    Set objWS = objWB.Worksheets(2)
    but now when i do
    objMacro = objXL.Run("Macro2")

    this in form B it says objects variable is not set??????

    I don't know how to call the same object in form B

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    You have to check if object is set first and rhen try to use it:
    VB Code:
    1. 'Form1
    2. If objXL Is Nothing Then
    3.     Set objXL = New Excel.Application
    4.     Set objWB = objXL.Workbooks.Open(FileName:=path + "TimesExcel.xls")
    5.     Set objWS = objWB.Worksheets(2)
    6. End If
    7.  
    8. 'Form2
    9. If objXL Is Nothing Then
    10.     Set objXL = New Excel.Application
    11.     Set objWB = objXL.Workbooks.Open(FileName:=path + "TimesExcel.xls")
    12.     Set objWS = objWB.Worksheets(2)
    13. Else
    14.     objXL.Run "Macro2"
    15. End If
    Note: when application ends - don't forget to close excel object and also reset all public objects to Nothing.

  6. #6
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: passing a excel sheet object from one form to another

    Originally posted by woow
    Hi,

    I have 2 forms lets assume its A and B. I want to pass a excel sheet object from form A to B so i can run a macro in the excel sheet from the form B.

    How can i pass a object from one form to another.

    thanks
    A good practice is to make your Worksheet a Private data member on Form A and then allow a Public Get Property on Form A, which can be accessed from other Forms:

    VB Code:
    1. ' frmFormA
    2. Private m_wksFormASheet as Excel.Worksheet
    3. '
    4. Public Property Get Worksheet() As Excel.Worksheet
    5.   Set Worksheet = wksFormASheet
    6. End Property

    Then from Form B:

    VB Code:
    1. frmFormA.Worksheet.Range("A1:C40").Select ' Etc...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width