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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Link to VB

Status
Not open for further replies.

svm

Programmer
Apr 26, 2001
44
US
Hi.

I have a VB Program that displays excel spreadsheet only the first time but if you close this file and open another spreadsheet file, it won't display data. What's wrong. Appreciate to any help you can give me. Thanks.

Form :

Option Explicit


Private Sub Command1_Click()
Set xlAPP = Nothing
Set xlSht = Nothing
excel_load
End Sub



Module :

Public xlSht As Object
Public xlAPP As excel.Application
Public xlBook As Object
Public xlDocName As String
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Sub xl_open(xl_fn As String)
Workbooks.Open (xl_fn)
End Sub


Public Sub xl_dia()
' Set CancelError is True
Form1.cdlxl.CancelError = True
On Error GoTo OpenErr
Form1.cdlxl.DialogTitle = "Open SpreadSheet"
Form1.cdlxl.InitDir = "e:\estimating\es-share"
Form1.cdlxl.Filter = "MS Excel (*.xls)|*.xls"
Form1.cdlxl.ShowOpen
OpenErr:
'User pressed the Cancel button
xlDocName = Form1.cdlxl.FileName
Exit Sub
End Sub

Public Sub excel_load()
xl_dia
Dim lResult As Long
lResult = FindWindow(vbNullString, "excel")
If lResult > 0 Then
lResult = ShowWindow(lResult, SW_RESTORE)
Else
If xlDocName <> &quot;&quot; Then
On Error Resume Next
Set xlAPP = GetObject(&quot;&quot;, &quot;Excel.Application&quot;)
If Err Then
Set xlAPP = CreateObject(&quot;&quot;, &quot;Excel.Application&quot;)
Err.Clear
End If
On Error Resume Next
If Err Then
MsgBox &quot;could Not Load Excel.&quot;, vbExclamation
End
Else
xlAPP.Visible = True
xl_open (xlDocName)
'xl_select &quot;Active Projects&quot;
'xlAPP.Workbooks.Activate
'xlSht.Activate
End If
End If
End If
End Sub

Public Function IsAppRunning() As Boolean
Dim objExcel As excel.Application
On Error Resume Next
Set objExcel = Nothing
Set objExcel = GetObject(, &quot;Excel.Application&quot;)
IsAppRunning = (Err.Number = 0)

If Not IsAppRunning Then
End If


Err.Clear
End Function



 
put the
xlapp.visible = true
stmt outside if structure and try
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top