DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    Using an Excel spreadsheet from VB

    [Originally posted by Chopper]

    Can anyone tell me how to use an Excel spreadsheet that has already been created, a template so to speak. I need to insert data into certain cells of a spreadsheet. What is the syntax for something like that.

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Using an Excel spreadsheet from VB

    [Originally posted by Greg DeBacker]

    You add a Reference to the Excel Object Library to your application (Project/References menu. Then select "Microsoft Excel 8.0 Object Library")

    You then create an Excel object variable and use it to manipulate Excel.

    Option Explicit
    'object variable
    Dim ApExcel As Object

    Private Sub Form_Click()
    ÿ ÿ 'Create the object
    ÿ ÿ Set ApExcel = CreateObject("Excel.application")
    ÿ ÿ
    ÿ ÿ ' Make Excel visible
    ÿ ÿ ApExcel.Visible = True
    ÿ ÿ
    ÿ ÿ 'Use this to add a new workbook
    ÿ ÿ 'ApExcel.Workbooks.Add
    ÿ ÿ
    ÿ ÿ 'use this to open an existing workbook
    ÿ ÿ ApExcel.Workbooks.Open ("c:\my documents\mybook.xls")
    ÿ ÿ
    ÿ ÿ 'Add Text to a Cell
    ÿ ÿ ApExcel.Cells(1, 1).Formula = "HELLO"
    ÿ ÿ
    ÿ ÿ 'change the borders.
    ÿ ÿ ApExcel.Range("A1:Z1").Borders.Color = RGB(0, 0, 0)
    ÿ ÿ
    ÿ ÿ 'adjust the column's width.
    ÿ ÿ ApExcel.Columns("A:AY").EntireColumn.AutoFit
    ÿ ÿ
    ÿ ÿ 'select a range
    ÿ ÿ ApExcel.Range("A:Z").Select
    ÿ ÿ
    ÿ ÿ 'change format
    ÿ ÿ ApExcel.Selection.NumberFormat = "0"
    ÿ ÿ
    End Sub

    Grex

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Using an Excel spreadsheet from VB

    [Originally posted by Chopper]

    Thanks you for you help! I may need some more later on. Thanks again!!

  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Using an Excel spreadsheet from VB

    [Originally posted by Anonymous]

    Exactly what I was looking for!!!ÿ I knew there was a way to specify formats.ÿ Here is how I specified the format of a column so as not to lose leading zeroes (in this instance, column A is the SSN).ÿ This can also be used to format numeric fields/rows/columns, and probably date field formats (I haven't tried it, yet). Thanks, again!

    ÿ ÿ 'select a range
    ÿ ÿ ApExcel.Range("A:A").Select
    ÿ ÿ 'change format
    ÿ ÿ ApExcel.Selection.NumberFormat = "000000000"

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links