Newsgroup: it.comp.lang.visual-basic
Titolo: creare file

Scusate come faccio a creare un file exel. ( o un
file office in genere).
Io lo faccio con il metodo open "file.xls" for output.....
C'è un modo diretto per farlo?
Ho letto con oggetto.saveas (non so a che
oggetto ci si riferisce)
Grazie in anticipo.


dipende da cosa ti serve, il "trucco" che si usa qui e' fare un file CSV (
cioe' un file di testo separato da un delimitatore comune ) che viene letto
e "intabellato" da exel, se invece ti serve proprio un file excel formattato
con tanto di formule e robina varia, li passo la palla, cmq credo che devi
referenziarti la versione di Excell che hai sulla macchina e poi da li agire
direttamente con l'oggetto "foglio" credo che 
Groups.google.com
 abbia la
risposta
-- 
-Gigio@Work(?)-

 ha scritto nel messaggio
news:lB3tb.20220$9_.7...@news1.tin.it...

Sample Code: The MakeXLObject Function
The MakeXLObject() function shown below uses the Excel.Sheet class to create
an instance of an Excel worksheet. It then populates it with data from an
Access database and saves the sheet for later use. "Later use" can be use by
the Access application that created it, or by users themselves. This
function, written in Access Basic, saves the worksheet as a standard
Microsoft Excel workbook (CSTMRPT.XLS) which contains a single sheet. Here
are a few things worth noting about the code (which starts on the next
page):
All the VBA for Microsoft Excel code is non-bold, so it's easy to spot.
When you launch Microsoft Excel through OLE automation, it's completely
invisible. In fact, it doesn't even appear on the Task List. But if you
monitor your free resources, it's obvious that it is open. To close
Microsoft Excel, use the following code:
objVariable.Application.[Quit]
Set objVariable = Nothing
Put Quit in the preceding code in square brackets so that the OLE automation
client that's creating the Microsoft Excel object, such as Access,
associates it with Microsoft Excel and not with the client's own application
object. Also, although it's not strictly necessary to use the second line
(and the keyword Nothing is undocumented in Access Basic), it's good
practice. You'll see why when you experiment with making the various classes
of Microsoft Excel objects visible. For example, to make the Microsoft
Excel.Sheet class visible, you have to declare the object variable as a
global variable. Of course, if you do this, the object variable remains in
memory at the end of the function... unless you set it to Nothing.
The MakeXLObject function declares the variable objXL globally by putting it
in the declarations section. To leave users in the newly created worksheet,
comment out the two lines that close the object and then add the following
line:
objXL.Application.Visible = True
When you test this, notice that the title of the sheet isn't "CSTMRPT.XLS,"
but "Object." When you close Access, this object closes too, although the
Microsoft Excel application itself remains open. (See the section titled
Getting Objects presented later for more information).
The following line of code prevents Microsoft Excel from prompting users if
the filename used for this report already exists:
If Dir$(strSavedReport) <> "" Then Kill strSavedReport
You need this line because the usual method for suppressing such
prompts--Application.DisplayAlerts = False--doesn't work under OLE
automation, and the SendKeys statement works only when Microsoft Excel is
visible.
Function MakeXLObject ()
    On Error GoTo ErrorMakeXLObject
    Dim dbSolution As Database
    Dim tdfCustomers As TableDef
    Dim rstCustomers As Recordset
    Dim intRow As Integer
    Dim intColumn As Integer
    Dim strSavedReport As String
    Set dbSolution = DBEngine(0)(0)
    Set tdfCustomers = dbSolution.TableDefs("Customers")
    Set rstCustomers = dbSolution.OpenRecordset("Investment")
'Set full path and name for saved Microsoft Excel object (i.e., report).
    strSavedReport = "C:\CSTMRPT.XLS"
'objXL is dimmed in Declarations section because otherwise
'you can't display the Sheet object and leave users in it.
'Create a Microsoft Excel worksheet object.
    Set objXL = CreateObject("Excel.Sheet")
'Display hourglass.
    DoCmd Hourglass True
'Add column headings from table.
    intRow = 1
    For intColumn = 1 To tdfCustomers.Fields.Count
        objXL.Cells(intRow, intColumn).Value =
tdfCustomers.Fields(intColumn - 1).Name
    Next intColumn
'Select first record
    intRow = 2
    rstCustomers.MoveFirst
    Do Until rstCustomers.EOF
        For intColumn = 1 To rstCustomers.Fields.Count
            objXL.Cells(intRow, intColumn).Value =
rstCustomers.Fields(intColumn - 1)
        Next intColumn
'Select other records and increment row counter
        rstCustomers.MoveNext
        intRow = intRow + 1
    Loop
'Check whether CSTMRPT.XLS exists, and kill it if it does;
'this prevents prompt to users to overwrite existing file.
'Excel's usual method, Application.DisplayAlerts = False,
'doesn't work under OLE automation. Also, SendKeys only works
'when the Microsoft Excel's application object is visible.
    If Dir$(strSavedReport) <> "" Then Kill strSavedReport
'Best fit Microsoft Excel columns and save sheet.
    objXL.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
    objXL.SaveAs strSavedReport
'Exit Microsoft Excel and clear object variable.
    objXL.application.[Quit]
    Set objXL = Nothing
ExitMakeXLObject:
    MsgBox "Saved Microsoft Excel object as C:\CSTMRPT.XLS.", 64,
"MakeXLObject"
'Turn off hourglass.
    DoCmd Hourglass False
    Exit Function
ErrorMakeXLObject:
    If Err <> 0 Then
        MsgBox "An error occurred in MakeObject. " & Chr$(13) &
"ERROR = " & Str$(Err), 48, "MakeObject"
    End If
    Resume ExitMakeXLObject
End Function