Torna al Thread

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(Application.StartupPath & "\Cartel1.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 = False excelObj.Parent.Windows(1).Visible = True ' Insert code to manipulate the test.xls file here. excelObj.Application.Sheets("Foglio1").Select() excelObj.Application.Range("A1").Select() excelObj.Application.ActiveCell.FormulaR1C1 = "TEST" excelObj.Application.Range("A2").Select() excelObj.Application.ActiveCell.FormulaR1C1 = "DA" excelObj.Application.Range("A3").Select() excelObj.Application.ActiveCell.FormulaR1C1 = "VB 2005" excelObj.Application.Range("A4").Select() excelObj.Application.ActiveCell.FormulaR1C1 = "AD" excelObj.Application.Range("A5").Select() excelObj.Application.ActiveCell.FormulaR1C1 = "EXCEL" 'With excelObj.Application.ActiveSheet.PageSetup ' .PrintTitleRows = "" ' .PrintTitleColumns = "" 'End With 'excelObj.Application.ActiveSheet.PageSetup.PrintArea = "" 'With excelObj.Application.ActiveSheet.PageSetup ' .LeftHeader = "" ' .CenterHeader = "" ' .RightHeader = "" ' .LeftFooter = "" ' .CenterFooter = "" ' .RightFooter = "" ' .LeftMargin = excelObj.Application.Application.InchesToPoints(0.787401575) ' .RightMargin = excelObj.Application.Application.InchesToPoints(0.787401575) ' .TopMargin = excelObj.Application.Application.InchesToPoints(0.984251969) ' .BottomMargin = excelObj.Application.Application.InchesToPoints(0.984251969) ' .HeaderMargin = excelObj.Application.Application.InchesToPoints(0.5) ' .FooterMargin = excelObj.Application.Application.InchesToPoints(0.5) ' .PrintHeadings = False ' .PrintGridlines = False ' .PrintComments = excelObj.Application.xlPrintNoComments ' .PrintQuality = 300 ' .CenterHorizontally = False ' .CenterVertically = False ' .Orientation = excelObj.Application.xlPortrait ' .Draft = False ' .PaperSize = excelObj.Application.xlPaperA4 ' .FirstPageNumber = excelObj.Application.xlAutomatic ' .Order = excelObj.Application.xlDownThenOver ' .BlackAndWhite = False ' .Zoom = 100 'End With 'excelObj.Application.Application.ActivePrinter = "" 'excelObj.Application.ActiveWindow.SelectedSheets.PrintOut(Copies:=1, ActivePrinter:= _ ' ":") '----------------------------------------------------------------------------------------------------------------------------- 'Questo codice servirebbe per salvare una copia del file Excel ma non lo fanno per cui si possono cancellare 'Dim xlNormal As String = "xlNormal" 'ChDir("I:\xls") 'excelObj.Application.ActiveWorkbook.SaveAs(FileName:="I:\xls\Test.xls", FileFormat:=xlNormal, _ 'Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _ 'CreateBackup:=False) '----------------------------------------------------------------------------------------------------------------------------- excelObj.Application.ActiveWorkbook.Save() excelObj.Application.ActiveWorkbook.Close(True) excelObj.Application.Quit() excelObj = Nothing End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click getExcel() End Sub End Class
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5