Explain how this works? (1 Viewer)

Rick Stanich

King and Supreme Ruler
Local time
Today, 11:55
Joined
May 13, 2009
Messages
93
Please explain how this works?

Can some one help explain how this works?
If that some one has time to add notes to the code. ;)

It works as shown, I'm just not sure how or what happens to the Excel object being created or used.
Hopefully, if I understand it better I may be able to modify this for my use.

Code:
'************ 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 IsAppRunning()
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 **********

All help is appreciated, hints, tips, examples, reading materials...
 
Last edited:

namliam

The Mailman - AWF VIP
Local time
Today, 20:55
Joined
Aug 11, 2003
Messages
11,695
fIsAppRunning("Excel")

If there is a running excel instance...


GetObject(, "Excel.Application")
Get the information about it to use later on

CreateObject("Excel.Application")
otherwize create a new excel session

Add a workbook and write "Hello world" in it...

Thats it really... What more do you need to know about it?
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 11:55
Joined
May 13, 2009
Messages
93
fIsAppRunning("Excel")

If there is a running excel instance...


GetObject(, "Excel.Application")
Get the information about it to use later on

CreateObject("Excel.Application")
otherwize create a new excel session

Add a workbook and write "Hello world" in it...

Thats it really... What more do you need to know about it?

Thanks, that definately helped. The text in Red, where does this workbook go? I would think it makes a default name "Book1" but it is never displayed.
Perhaps Im am seeing to much into this and dont need to know about the workbook?
 

namliam

The Mailman - AWF VIP
Local time
Today, 20:55
Joined
Aug 11, 2003
Messages
11,695
The excel application is JUST the excel application, no workbook is contained within it...

Also if the excel application excists already you wouldnt want to write into the excisting book(s) in danger of destroying data that is already there. Hence the creation of a new book.
It may be the book is hidden (not sure on that)
Try objActiveWkb.Visible = True
To make it visible... Then again it may be going to fast ??

Have you tried selecting a word you dont understand (like "GetObject") and pressing "F1" and reading the access help about it??
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 11:55
Joined
May 13, 2009
Messages
93
Yes, I am just confused on how it writes to a workbook and where the workbook is located.

Thank you for your help ;)
 

namliam

The Mailman - AWF VIP
Local time
Today, 20:55
Joined
Aug 11, 2003
Messages
11,695
objActiveWkb.Close savechanges:=False

What does above line do you think?
 

Users who are viewing this thread

Top Bottom