Writing to Files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nevo

    #1

    Writing to Files

    Can I write to files, say for example, to an excel spreadsheet and have it
    written in a certain order or in certain cells?

    Thanks,
    VAL


  • Ben Hall

    #2
    Re: Writing to Files

    On Tue, 30 Sep 2003 05:55:16 -0400, "Nevo" <victorlopez@be llsouth.net>
    wrote:
    [color=blue]
    >Can I write to files, say for example, to an excel spreadsheet and have it
    >written in a certain order or in certain cells?[/color]

    Yes, it is possible.

    You need to create a reference to the Excel object in your program. In
    the Visual Basic editor, select menu Project | References and scroll
    down and tick Microsoft Excel, click OK.

    Here's a simple program that writes values to all cells in the range
    A1:J10 in a new spreadsheet. It then save the new spreadsheet.

    Private Sub cmdCommit_Click ()

    Dim xlApp As Excel.Applicati on
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet

    Set xlApp = New Excel.Applicati on
    Set xlBook = xlApp.Workbooks .Add
    Set xlSheet = xlBook.Workshee ts.Add

    For i = 1 To 10
    For j = 1 To 10
    xlSheet.cells(i , j) = i * j
    Next j
    Next i

    xlBook.SaveAs ("c:\temp\VBShe et1.xls")

    End Sub

    Comment

    • Andreas

      #3
      Re: Writing to Files

      To write to an excel spreadsheet, you need excel installed on the system.

      Here is some code for you:

      ' Need to add Component "Microsoft Excel 9.0 Object Library"
      ' Declare object variables for Microsoft Excel,
      ' application workbook, and worksheet objects.

      Dim xlApp As Excel.Applicati on
      Dim xlBook As Excel.Workbook
      Dim xlSheet As Excel.Worksheet

      ' Assign object references to the variables. Use
      ' Add methods to create new workbook and worksheet
      ' objects.

      Set xlApp = New Excel.Applicati on
      Set xlBook = xlApp.Workbooks .Add
      Set xlSheet = xlBook.Workshee ts.Add

      ' Assign the values Fron double Matrix ExportData to
      ' Microsoft Excel cells.
      Dim DataStartAtRow As Integer

      xlApp.DisplayAl erts = False

      'To set a singel cell>
      xlSheet.Cells(1 , 1).value = "Hello world!"

      'To transfer a vector
      xlSheet.Range(x lSheet.Cells(1, 1), xlSheet.Cells(5 ,5)) = myMatrix5x5

      ' Use the Formula method to add the values in
      ' Microsoft Excel.
      ' xlSheet.Cells(3 , 1).Formula = "=R1C1 + R2C1"

      ' Save the Worksheet.
      xlSheet.SaveAs exportFileName
      ' Close the Workbook
      xlBook.Close
      ' Close Microsoft Excel with the Quit method.
      xlApp.Quit

      ' Release the objects.
      Set xlApp = Nothing
      Set xlBook = Nothing
      Set xlSheet = Nothing

      Good luck!

      /Andreas Lundgren - Sweden

      "Nevo" <victorlopez@be llsouth.net> wrote in message news:<Asceb.193 18$T65.4538@big news4.bellsouth .net>...[color=blue]
      > Can I write to files, say for example, to an excel spreadsheet and have it
      > written in a certain order or in certain cells?
      >
      > Thanks,
      > VAL[/color]

      Comment

      Working...