Home  |   French  |   About  |   Search  | mvps.org  

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

In Memoriam

Terms of Use


VB Petition

Modules: Sample Excel Automation

Author(s)
Dev Ashish

A sample Sub to demonstrate Excel Automation.

Note: Also pick up the fIsAppRunning function from the API section.

'************ Code Start **********
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Sub sTestXL()
Dim objXL As Object
Dim strWhat As String, boolXL As Boolean
Dim objActiveWkb As Object

    If fIsAppRunning("Excel") Then
        Set objXL = GetObject(, "Excel.Application")
        boolXL = False
    Else
        Set objXL = CreateObject("Excel.Application")
        boolXL = True
    End If
    
    objXL.Application.workbooks.Add
    Set objActiveWkb = objXL.Application.ActiveWorkBook
    
    With objActiveWkb
        .Worksheets(1).Cells(1, 1) = "Hello World"
        strWhat = .Worksheets(1).Cells(1, 1).value
    End With
    
    objActiveWkb.Close savechanges:=False
    
    If boolXL Then objXL.Application.Quit
    
    Set objActiveWkb = Nothing: Set objXL = Nothing
    MsgBox strWhat
End Sub
'************ Code End **********

1998-2010, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer