Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Strange behavior opening Excel
Message
 
À
Tous
Information générale
Forum:
Visual Basic
Catégorie:
Autre
Titre:
Strange behavior opening Excel
Divers
Thread ID:
00290086
Message ID:
00290086
Vues:
48
In a new project:
-Create an Excel file called "C:\Temp\Bax.xls";
-add a reference to Excel;
-add a command button to the form;
-add the following code to your project:

Option Explicit

Private gobjExcel As Excel.Application

Private Sub Command1_Click()
If Not OuvrirExcel("C:\Temp\Bax.xls") Then End

With gobjExcel
.Worksheets("Sommaire").Activate
.Range("Date").Value = Date
.Range("A1").Select
End With

gobjExcel.Visible = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
ExcelQuit
End Sub

Private Function OuvrirExcel(ByVal pstrFichierTemplate As String) As Boolean

On Error GoTo ErrorHandler

OuvrirExcel = False

ExcelOpen
If gobjExcel Is Nothing Then Exit Function

gobjExcel.Workbooks.Open pstrFichierTemplate

OuvrirExcel = True

ErrorHandler:
MsgBox "Impossible d'ouvrir le fichier demandé." & _
vbCrLf & vbCrLf & _
"Code d'erreur: " & Err.Number & _
vbCrLf & _
"Description: " & Err.Description, _
vbExclamation + vbOKOnly
End Function

Public Sub ExcelCloseDoc()
Dim objWorkBookTmp As Excel.Workbook

On Error Resume Next

If gobjExcel.Workbooks.Count > 0 Then
For Each objWorkBookTmp In gobjExcel.Workbooks
objWorkBookTmp.Close False
Next
End If
End Sub

Private Sub ExcelOpen()
' Getobject function called without the first argument
' returns a reference to an instance of the application.
' If the application isn't running, an error occurs.

On Error Resume Next

Set gobjExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear ' Clear Err object in case error occurred.
Set gobjExcel = New Excel.Application
End If
ExcelCloseDoc
End Sub

Public Sub ExcelQuit()
On Error Resume Next

If gobjExcel Is Nothing Then Exit Sub
ExcelCloseDoc
gobjExcel.Quit
Set gobjExcel = Nothing
End Sub

Now run it this way:
-Click on the button: Excel will open.
-Close the workbook (not Excel - only the workbook).
-Click on the button again: Excel will open the workbook again.
-Up to here everything is going as it is suppose to go.
-Now close Excel.
-Click on the button again. What I see now is the Excel frame (title bar, menu, status bar, ...) but not the worksheet!

What can I do to correct it?
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform