|
-
Jul 21st, 2004, 08:35 AM
#1
Thread Starter
Lively Member
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
-
Jul 21st, 2004, 08:40 AM
#2
You may just declare your excel object as Public at the module level and use it throughout your app lifecycle.
-
Jul 21st, 2004, 08:43 AM
#3
Thread Starter
Lively Member
I am a newbie I am lost. Could u please give me an example
-
Jul 21st, 2004, 09:11 AM
#4
Thread Starter
Lively Member
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
-
Jul 21st, 2004, 09:39 AM
#5
You have to check if object is set first and rhen try to use it:
VB Code:
'Form1
If objXL Is Nothing Then
Set objXL = New Excel.Application
Set objWB = objXL.Workbooks.Open(FileName:=path + "TimesExcel.xls")
Set objWS = objWB.Worksheets(2)
End If
'Form2
If objXL Is Nothing Then
Set objXL = New Excel.Application
Set objWB = objXL.Workbooks.Open(FileName:=path + "TimesExcel.xls")
Set objWS = objWB.Worksheets(2)
Else
objXL.Run "Macro2"
End If
Note: when application ends - don't forget to close excel object and also reset all public objects to Nothing.
-
Jul 21st, 2004, 11:23 AM
#6
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:
' frmFormA
Private m_wksFormASheet as Excel.Worksheet
'
Public Property Get Worksheet() As Excel.Worksheet
Set Worksheet = wksFormASheet
End Property
Then from Form B:
VB Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|