Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using VB6 to write Excel Spreadsheets -

Posted on 2016-11-25
5
Medium Priority
?
71 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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