Solved

Visual basic 6 to excel application

Posted on 2001-09-11
5
273 Views
Last Modified: 2013-11-25
I wish to open an excel application from Visual basic 6.

I can create and save using the following code:
Private Sub Form_Load()
Dim obExcelApp As Object
Dim obWorkSheet As Object
Set obExcelApp = CreateObject("excel.Application")
obExcelApp.Workbooks.Add
Set obWorkSheet = obExcelApp.ActiveSheet
obWorkSheet.Cells(1, 1).Value = "Date"
obWorkSheet.Cells(1, 2).Value = "Num"
obWorkSheet.Cells(2, 1).Value = 21923.44
obWorkSheet.Cells(2, 2).Value = "April"
obExcelApp.Save ("C:\Donations1\Register.XLS")
End Sub

I wish to open the worksheet and edit it on screen.
0
Comment
Question by:JAwilson
5 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 6475064
Hi JAWilson, you want to show excel on the screen while editing in code ? then add one line with visible

Private Sub Form_Load()
Dim obExcelApp As Object
Dim obWorkSheet As Object
Set obExcelApp = CreateObject("excel.Application")
obExcelApp.Visible = True
obExcelApp.Workbooks.Add
Set obWorkSheet = obExcelApp.ActiveSheet
obWorkSheet.Cells(1, 1).Value = "Date"
obWorkSheet.Cells(1, 2).Value = "Num"
obWorkSheet.Cells(2, 1).Value = 21923.44
obWorkSheet.Cells(2, 2).Value = "April"
obExcelApp.Save ("C:\Donations1\Register.XLS")
End Sub

else let me know,

:O)Bruintje
0
 

Accepted Solution

by:
etienne14 earned 100 total points
ID: 6475074
If you really want to open and edit the worksheet try this

Shell "excel.exe c:\donations\register.xls",vbmaximizedfocus

regards
etienne14
0
 
LVL 2

Expert Comment

by:Lunchy
ID: 6478465
JAwilson, it is to your advantage to reject an answer immediately if it does not completely satisfy your requirements.

Lunchy
Friendly Neighbourhood Community Support Moderator
0
 

Expert Comment

by:hslow
ID: 6481501
hi,
you can open and edit yr excel file as below:

Option Explicit
Public appWorld As Excel.Application
Public wbWorld As Excel.Workbook


Sub OpenExcelFile()
On Error Resume Next 'ignore errors
    Set appWorld = GetObject(, "Excel.Application")
'look for a running copy of Excel
    If Err.Number <> 0 Then 'If Excel is not running then
      Set appWorld = CreateObject("Excel.Application")
     'run it
    End If
    Err.Clear   ' Clear Err object in case error occurred.
   
    On Error GoTo 0 'Resume normal error processing

    Set wbWorld = appWorld.Workbooks.Open("C:\Donations1\Register.XLS")
End Sub

Private Sub Form_Load()
Dim obWorkSheet As Excel.Worksheet

OpenExcelFile()
appWorld .Visible = True
appWorld .Workbooks.Add
Set obWorkSheet = appWorld.ActiveSheet
obWorkSheet.Cells(1, 1).Value = "Date"
obWorkSheet.Cells(1, 2).Value = "Num"
obWorkSheet.Cells(2, 1).Value = 21923.44
obWorkSheet.Cells(2, 2).Value = "April"
obExcelApp.Save ("C:\Donations1\Register.XLS")

End Sub

Hope this can help you :-)
Regards,
hslow
0
 

Author Comment

by:JAwilson
ID: 6515230
I used hslows comment to create the following code which works.
Private Sub Command5_Click()
Dim i As Integer
Dim j As Integer
Dim Numb1 As Integer
Dim Numb2 As Integer
Dim Numb3 As Integer
Dim obWorkSheet As Excel.Worksheet
OpenExcelFile
'obWorkSheet.Open ("c:\Donations1" & "\Register.XLS")
Set wbWorld = appWorld.Workbooks.Open("c:\Donations1" & "\Register1.XLS")
'Open("c:\donations1\register1.exl")
'OpenExcelFile
appWorld.Visible = True
'appWorld.Workbooks.Add
Set obWorkSheet = appWorld.ActiveSheet
obWorkSheet.Cells(1, 1).Value = "Date"
obWorkSheet.Cells(1, 2).Value = "Numb"
obWorkSheet.Cells(1, 3).Value = "DisbID"
'obWorkSheet.Cells(2, 1).Value = 9 / 11 / 1
obWorkSheet.Cells(1, 3).Value = "Account"
Dim DbDonations1 As New ADODB.Connection
Dim rsReceipts As New ADODB.Recordset
Dim rsDisbursements As New ADODB.Recordset
Dim rsTransfers As New ADODB.Recordset
Dim rsAssigned As New ADODB.Recordset
DbDonations1.Open "Donations1"
Set rsAssigned = rsReceipts
Data1.Recordset.MoveLast
Numb1 = Data1.Recordset("receiptID")
'MsgBox "Numb1= " & Numb1
For i = 1 To Numb1
Text3.Text = i
Set rsAssigned = DbDonations1.Execute("Select * From Receipts where receiptID = '" & Text3.Text & "'")
On Error GoTo Error_Fix
j = i + 1
obWorkSheet.Cells(j, 1).Value = rsAssigned("CreationDate"):
obWorkSheet.Cells(j, 2).Value = rsAssigned("AccountID"):
obWorkSheet.Cells(j, 3).Value = rsAssigned("Description")
Next
Set rsAssigned = rsDisbursements
Data3.Recordset.MoveLast
Numb2 = Data3.Recordset("DisbID")
'MsgBox "numb2 =" & Numb2
For i = 1 To Numb2
Text5.Text = i
Set rsAssigned = DbDonations1.Execute("Select * From Disbursements where DisbID = '" & Text5.Text & "'")
On Error GoTo Error_Fix
j = i + Numb1
obWorkSheet.Cells((j + 1), 1).Value = rsAssigned("CreateDate"):
obWorkSheet.Cells((j + 1), 2).Value = rsAssigned("AcctID"):
obWorkSheet.Cells((j + 1), 3).Value = rsAssigned("DisbID")
Next
Set rsAssigned = rsTransfers
Data4.Recordset.MoveLast
Numb3 = Data4.Recordset("TransferID")
'MsgBox "numb3 =" & Numb3
For i = 1 To Numb3
Text7.Text = i
Set rsAssigned = DbDonations1.Execute("Select * From Transfers where TransferID = '" & Text7.Text & "'")
On Error GoTo Error_Fix
j = i + (Numb1 + Numb2)
obWorkSheet.Cells((j + 1), 1).Value = rsAssigned("TransferDate"):
obWorkSheet.Cells((j + 1), 2).Value = rsAssigned("TransDescription"):
obWorkSheet.Cells((j + 1), 3).Value = rsAssigned("TransferID")
Next

Error_Fix:
Err.Clear
End Sub
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 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

14 Experts available now in Live!

Get 1:1 Help Now