Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

opening an existing excel file in basic extra!!!!! 2

Status
Not open for further replies.

winnier

Technical User
Nov 7, 2002
7
0
0
US
Would someone please post the code in Extra basic for opening an existing Excel file? So far, all of the code examples I've seen (and used) creates a new Worksheet at each run. I'd appreciate it mucho.
 
Following is the code that I use to open an Excel 2000 file. Hope this helps you.

GC
############### CODE FOLLOWS #############


Sub Main

' Get the main system object
Dim Sessions As Object
Dim System As Object
Set System = CreateObject("EXTRA.System") ' Gets the system object

If (System is Nothing) Then
Msgbox "Could not create the EXTRA System object. Stopping macro playback."
STOP
End If

Set Sessions = System.Sessions

If (Sessions is Nothing) Then
Msgbox "Could not create the Sessions collection object. Stopping macro playback."
STOP
End If

' Set the default wait timeout value
g_HostSettleTime = 300 ' milliseconds
OldSystemTimeout& = System.TimeoutValue
If (g_HostSettleTime > OldSystemTimeout) Then
System.TimeoutValue = g_HostSettleTime
End If

' Get the necessary Session Object
Dim Sess0 As Object
Set Sess0 = System.ActiveSession

If (Sess0 is Nothing) Then
Msgbox "Could not create the Session object. Stopping macro playback."
STOP
End If

If Not Sess0.Visible Then Sess0.Visible = TRUE

Sess0.Screen.WaitHostQuiet(g_HostSettleTime)
' Declare variables to contain the OLE objects
Dim objExcel As Object
Dim objWorkBook As Object
Dim objChart As Object

On Error Resume Next

' Attempt to get a reference to an open instance of Excel 97/2000
Set objExcel = GetObject(, "Excel.Application")
If objExcel Is Nothing Then
'If GetObject failed, open a new instance of Excel 97/2000
Set objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
MsgBox ("Could not open Excel 97/2000.")
Exit Sub
End If
End If

' Make Excel visible on the screen
objExcel.Visible = True

' Create a new Workbook
' Set objWorkBook = objExcel.Workbooks.Add



'************************************ change yourfile.xls to the file you wish to open.***************
'************************************ change yourfile.xls to the file you wish to open.***************
'************************************ change yourfile.xls to the file you wish to open.***************
'************************************ change yourfile.xls to the file you wish to open.***************

' Open a Workbook
Set objWorkBook = objExcel.Workbooks.Open("yourfile.xls")


'************************************ change yourfile.xls to the file you wish to open.***************
'************************************ change yourfile.xls to the file you wish to open.***************
'************************************ change yourfile.xls to the file you wish to open.***************
'************************************ change yourfile.xls to the file you wish to open.***************


If objWorkBook Is Nothing Then
MsgBox ("Could not open a new Excel workbook.")
objExcel.Quit
Exit Sub
End If

' Since the Quit method of objExcel has not been called,
' Excel will remain open after this Sub ends.
' To close out Excel, unremark the following 4 lines of code.

'objWorkBook.Close
'objExcel.Quit
'set objWorkBook = Nothing
'set objExcel = Nothing

End Sub
 
Just a little tip.... I have done this before it depends on the version of Excel you have, the api call is different for each version of Excel. You can find some more code on the attachmate site (log on as guest) for another version of Excel.
 
How exactly did you (iluv2ski) do this? I am using Excel 97. I am programming in Extra Basic. I,ve checked out the attachmate site and seen most of their examples. I can open a new instance of Excel and then move data from my host to the worksheet and vice versa, I can even "open" an existing worksheet but all I can do with this is select and paste cells. which then just sits there and doesn't respond to any code but I can't open an existing Excel file that then becomes the active worksheet. Surely someone has the answer and there must be another with this need. please keep the suggestions flowing.
 
I also have run into this, as most of the work I do is done on Excel spreadsheets. I take the data from the spreadsheet and have it copy/pasted to the Host session. I used this macro from the Attachmates website.

' Open a Workbook
Set objWorkBook = objExcel.Workbooks.Open("yourfile.xls")

This line of code works, however, since I use the Start: GoTo Start: command in my macros it does not close the instance of Excel it previously opened. I have tried the Close and Quit calls for the Object with no positive results.

I am trying to get it to move down each row also, but havent quite figured out how to get it to do that yet. On a more positive note, I did manage to get a result from the screen to paste into Excel, but not in the Workbook that my data is in.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top