Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using VB6 to write Excel Spreadsheets -

Posted on 2016-11-25
5
Medium Priority
?
76 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 22

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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

824 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