Eccomi... scusate il ritardo ma ho dovuto rifare...
codice:
'Uso la Microsoft Excel 11.0 Object Library
Option Explicit
Dim ApplExcel As New Excel.Application 'Creo questa santa variabile...
Dim WorkExcel As New Excel.Workbook 'Creo la variabile (come mi hai suggerito)nella quale alloco il workbook...
Dim ExcRow As Byte
Dim ExcCol As Byte
'Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
'Private Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long
Private Sub cmdCreateSheet_Click()
On Error GoTo GeErr
'Dim myHandle As Long 'Dove Allocavo l'handle dell'applicazione che aprivo...
'ApplExcel.Workbooks.Add 'Prima il WorkBook non lo inserivo in una variabile...
'myHandle = ApplExcel.hWnd
Set ApplExcel = CreateObject("Excel.Application")
Set WorkExcel = ApplExcel.Workbooks.Add 'Setto la variabile con un nuovo workbook
With WorkExcel.ActiveSheet 'Forse inizio a capire dove ho sbagliato...
For ExcRow = 1 To 20
For ExcCol = 1 To 7
.Cells(ExcRow, ExcCol).Value = Rnd(1) * 10 'Vabbè...
Next ExcCol
Next ExcRow
ExcCol = ExcCol - 1
ApplExcel.Visible = True 'Visualizzo il foglio per rendere valido il settaggio delle opzioni celle sull'activesheet...
For ExcRow = 1 To 20 '...effetto bellino
If ExcRow Mod 2 = 0 Then
.Range(Cells(ExcRow, 1), Cells(ExcRow, ExcCol)).Interior.Color = &H80FF& 'Setto alcune opzioni delle celle sfruttando la proprietà range (...invece che usare un ciclo...)
Else
.Range(Cells(ExcRow, 1), Cells(ExcRow, ExcCol)).Interior.Color = &H80C0FF
End If
Next ExcRow
ExcRow = ExcRow - 1
.Range(Cells(1, 1), Cells(ExcRow, ExcCol)).Borders.LineStyle = xlContinuous
.Range(Cells(1, 1), Cells(ExcRow, ExcCol)).Font.Name = "Arial"
.Range(Cells(1, 1), Cells(ExcRow, ExcCol)).Font.Size = 10
End With
WorkExcel.Close '...
Set WorkExcel = Nothing
Set ApplExcel = Nothing
'TerminateProcess myHandle, 0
'CloseHandle (myHandle)
Exit Sub
GeErr:
Select Case Err.Number
Case Is = 1004 'Quel fatidico errore che mi si presenta alla seconda elaborazione del foglio excel
Err.Number = 0
Resume Next
Case Else
MsgBox Err.Number & " " & Err.Description
Exit Sub
End Select
End Sub
Private Sub Form_Load()
Me.Left = (Screen.Width - Me.Width) / 2
Me.Top = (Screen.Height - Me.Height) / 2
End Sub
NOTA
- Se metto come commento
codice:
Set AppExcel = CreateObject ("Excel.Application")
Set AppExcel = Nothing
Mi funziona....
Why ??
Can SomebodY HelpME??
Grazie Anticipate...