Results 1 to 3 of 3

Thread: Microsoft Office Late Binding

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    107

    Microsoft Office Late Binding

    OK, guys I use what I think is a very good way of setting up the automation process with office.... here go's - dont be too harsh if you dont like it.
    VB Code:
    1. Public oApp as Object
    2.  
    3. Public Function ExcelOpen() As Boolean
    4.   ExcelOpen = False
    5.   Set oApp = Nothing
    6.   On Error Resume Next
    7.   Set oApp = GetObject(, "Excel.application")
    8.   If Err.Number > 0 Then
    9.       Set oApp = CreateObject("Excel.Application")
    10.       Err.Clear
    11.   End If
    12.   ExcelOpen = Not (oApp Is Nothing)
    13.   If Not ExcelOpen Then
    14.       MsgBox "Are you sure Microsoft Excel is installed on your computer?"
    15.   End If
    16. End Function

    This can be used to open up any office application, just copy and change the "Excel" bit.
    I am not suffering from insanity......... ...........I am loving every minute of it.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Microsoft Office Late Binding

    That looks good, but there is room for improvement:

    The thing I noticed first is that you dont have any "close" process, so the object variables will not get cleared (so waste memory) and/or the application (Excel) will not close properly (which you may not notice until you shut down the PC). Of course you may have a seperate function to do this, but just haven't shown it to us!

    The other issue is your error handling (or rather, ignoring), which isn't quite right. It is understandable that you are using Resume Next for the GetObject part, however you should return to "normal service" as soon as possible afterwards - using a proper error handler to deal with any issues that arise, and give apropriate error messages to the user.


    It also seems to me that it may be better to check (at the start of the function) if the application object (oApp) already refers to an instance of the application, and if so simply activate that one rather than creating another.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    107

    Re: Microsoft Office Late Binding

    I did say "don't be harsh"...

    LOL, no thats great, I accept your valid points. I do have another function to Close up everything... will probably append the above post when I get chance.

    The program this was written for is a document managing utility where lots of documents are opoened up one after the other... rather than closing the object all the time, it was much faster to leave it open - if that makes any sense...

    anyways, will take your points on board... many thanks
    I am not suffering from insanity......... ...........I am loving every minute of it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width