[VB 2005 Express] Non riesco a chiudere la procedura.

mercoledì 22 ottobre 2008 - 17.53

alexmed Profilo | Guru

Avrei bisogno di una mano a chiudere questa procedura
L'obiettivo è:
1.Capire quale versione di Excel è installata
2.Aprire un file esistente
3.Riempire alcunne celle
4.Salvare il file con nome
5.Stamparlo
6.Chiudere tutto

Fino al punto 3 tutto bene, ma quando gli dico di salvare con nome, Excel mi risponde: "Salvare le modifiche a Preventivo.xls?"
E del file "Test.xls" nessuna traccia.

E ha dire che proprio Excel mi ha fatto "innamorare" del Visual Basic!!!!

Ecco il codice che ho scopiazzato di quà e di là


Option Strict Off

Public Class frmMAIN

Declare Function findWindow Lib "user32.dll" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As Long) As Long
Declare Function sendMessage Lib "user32.dll" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Public Sub detectExcel()
' Procedure detects a running Excel and registers it.
Const WM_USER As Long = 1024
Dim hWnd As Long
' If Excel is running, this API call returns its handle.
hWnd = findWindow("XLMAIN", 0)
If hWnd = 0 Then
' 0 means Excel is not running.
Exit Sub
Else
' Excel is running, so use the sendMessage API function
' to enter it in the Running Object table.
sendMessage(hWnd, WM_USER + 18, 0, 0)
End If
End Sub

Public Sub getExcel()

Dim excelObj As Object

Dim excelWasNotRunning As Boolean
' Test to see if a copy of Excel is already running.
On Error Resume Next
' GetObject called without the first argument returns a
' reference to an instance of the application. If the
' application is not already running, an error occurs.
excelObj = GetObject(, "Excel.Application")
If Err().Number <> 0 Then excelWasNotRunning = True
Err().Clear()
' If Excel is running, enter it into the Running Object table.
detectExcel()
' Set the object variable to refer to the file you want to use.
excelObj = GetObject("F:\xls\Preventivo.xls")
' Show Excel through its Application property. Then show the
' window containing the file, using the Windows collection of
' the excelObj object reference.
excelObj.Application.Visible = True
excelObj.Parent.Windows(1).Visible = True
' Insert code to manipulate the test.xls file here.

Dim Ragio As String = "Ragione Sociale"
Dim Ind As String = "Indicativo"
Dim Loc As String = "Località"
Dim Cap As String = "CAP"
Dim Prov As String = "Provincia"
Dim Iso As String = "ISO"
Dim Piva As String = "PARTITA IVA"
Dim CodF As String = "CodIce Fiscale"
Dim Tel As String = "Telefono"
Dim Mail As String = "E-Mail"

excelObj.Application.Sheets("PREVENTIVO").Select()
excelObj.Application.Range("A1").Select()
excelObj.Application.ActiveCell.FormulaR1C1 = Ragio
excelObj.Application.Range("A2").Select()
excelObj.Application.ActiveCell.FormulaR1C1 = Ind
excelObj.Application.Range("A3").Select()
excelObj.Application.ActiveCell.FormulaR1C1 = Cap
excelObj.Application.Range("A4").Select()
excelObj.Application.ActiveCell.FormulaR1C1 = Loc
excelObj.Application.Range("A5").Select()
excelObj.Application.ActiveCell.FormulaR1C1 = Prov

excelObj.Application.ActiveWorkbook.SaveAs(FileName:="F:\xls\Test.xls", FileFormat:="xlNormal", _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False)

excelObj.Application.ActiveWorkbook.Close(False)
excelObj.Application.Quit()


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
getExcel()
End Sub


GRAZIE A TUTTI!



PS
Naturalmente non voglio usare:
Microsoft.Office.Interop.Excel

Questo il link della guida in linea da dove tutto è iniziato:
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.it/dv_vbalr/html/47dd01cb-9468-481e-be7e-55f69a744635.htm


Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5