Solved

Using VB6 to write Excel Spreadsheets -

Posted on 2016-11-25
5
39 Views
Last Modified: 2016-11-30
When using VB6 to write Excel Spreadsheets, I am running into a problem where I have to span one excel file opened as an object across 2 subroutines. I have a couple of subroutines that I need to call several times, to I really need to use them.

It acts as though the object won't span out of the originating procedure.

Example:
Dim oxl As Object, oWB As Object, IStartedXL As Boolean
    Set oxl = GetObject(, "excel.application")
    On Error GoTo 0
    If oxl Is Nothing Then
        Set oxl = CreateObject("excel.application")
        IStartedXL = True
        End If
    Set oWB = oxl.Workbooks.Add
    oxl.UserControl = True
   
oxl.Columns("A:A").Select
oxl.Selection.ColumnWidth = 7.5
Call TestOne()


Public Sub testone()
If NoCostError > 4 And Len(Trim(CostError(4))) > 2 Then
    Flag = 0
    For i = 1 To NoCostError
        If Trim(Len(CostError(i))) > 2 Then Flag = 1
        If Flag = 1 And Len(Trim(CostError(i))) > 0 Then
            Irow = Irow + 1
            oxl.Cells(Irow, 1).Value = CostError(i)   <==============   Error occurs here
        End If
    Next i
End If
Irow = Irow + 2
End Sub

if I put the code in the subroutine code into the calling procedure (not calling a subroutine), it works fine. Any ideas would definitely be appreciated.

Thank you,

WilsonB
0
Comment
Question by:wilson bailey
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:JesterToo
ID: 41902449
What specific error are you getting?  Try putting a error-handling wrapper around the statement to see if it reveals anything useful.  Also, have you tried passing the oxl object explicitly to the subroutine (maybe byref)?
0
 

Author Comment

by:wilson bailey
ID: 41902635
I just tried the bits I sent you. I would think something like the "by ref" is what I need, but I really don't know how to accomplish that.
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 41902684
one way as a last resort is to make the oXL & oWB objects global, but definitely should be passing the parent object.

Also, your code
oxl.Cells(Irow, 1).Value 

Open in new window

doesn't seem right.

the Cells property is part of a worksheet, not an Excel object.
while it may be using ActiveWorkbook.ActiveSheet by default, it may be part of the issue.

in my opinion, it should be
oXL.ActiveWorkbook.ActiveSheet.Cells(lrow,1).Value

Open in new window

0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
ID: 41902685
Sub Main()
	Dim oxl As Object, oWB As Object, IStartedXL As Boolean
	dim oWS as Object
    Set oxl = GetObject(, "excel.application")
    On Error GoTo 0
    If oxl Is Nothing Then
        Set oxl = CreateObject("excel.application")
        IStartedXL = True
        End If
    Set oWB = oxl.Workbooks.Add
    oxl.UserControl = True
    
	set oWS = oWB.Activesheet
	oWS.Columns("A:A").Select
	oWS.Selection.ColumnWidth = 7.5
	Call TestOne(oWS)
End Sub

Public Sub testone(oWS as object)
	If NoCostError > 4 And Len(Trim(CostError(4))) > 2 Then
		Flag = 0
		For i = 1 To NoCostError
			If Trim(Len(CostError(i))) > 2 Then Flag = 1
			If Flag = 1 And Len(Trim(CostError(i))) > 0 Then
				Irow = Irow + 1
				oWS.Cells(Irow, 1).Value = CostError(i)   
			End If
		Next i
	End If
	Irow = Irow + 2
End Sub

Open in new window

0
 

Author Closing Comment

by:wilson bailey
ID: 41908169
Thank you for your help. On this same project, I am running into a problem when  copy use filecopy (also tried FSO) - The file copies, but gives an file access error. I'm copying to a server, by the way. Any ideas.

Thank you,

WilsonB
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now