Results 1 to 8 of 8
  1. #1
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679

    Instantiated Excel applications fails alternately

    This is driving me crazy!



    I am instantiating an Excel application from an Access project. All works well until the following code is executed

    Code:
    Private Sub SlantColumnHeadings()
    '    MxlsWorksheet.Range(Cells(1, 3), Cells(1, 8)).Select
    '    MxlsApp.Goto "Activities"
    '    With Range("Activities")
        MxlsWorksheet.Range("$C$1:$I$1").Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 60
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
    End Sub
    Now, this code works on the first, third, fifth, ... executions but fails on the second, fourth, ... executions. If it's relevant, the error code is 1044 if I'm using the Range object/method or otherwise that the Selection is Nothing. You can see the various different ways I have tried from the commented-out statements. All have the same effect. The code resides in an Access module.

    Since the code works 50% of the time, I conclude it's OK; there's something else amiss. I cannot believe there is a hidden toggle in the underlying software, so what is different about the even numbered executions? Here's a summary of my actions:

    1. Software execution OK
    2. Software fails - reset Access VBA code
    3. Software execution OK
    4. Software fails - reset Access VBA code
    5. ...


    So is it the VBA Reset that's clearing whatever prevents the code executing successfully a second time? Has anyone encountered this or similar before and, more important, what was the solution? My Excel Application is Set .. New .. each time and all other Excel objects are set based on the new application so I can't believe it's a carry-over instance that's causing this.

    Code:
    Set MxlsApp = New Excel.Application
        Set MxlsWorkbook = MxlsApp.Workbooks.Add
        Set MxlsWorksheet = MxlsApp.Worksheets(1)
    Ideas anyone?

  2. #2
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Well, I have solved my problem. I can't say exactly what was happening but it is caused by having multiple copies of Excel simultaneously. I changed the instatiating code to be as follows (yes, I know, I won't leave it like this ) and it all works!

    Code:
      On Error Resume Next
        Set MxlsApp = GetObject(, "Excel.Application")
        If Err.Number = 429 Then
            Debug.Print "creating new app"
            Set MxlsApp = CreateObject("Excel.Application")
        End If
    The odd thing is that the createobject branch is taken only on the first execution. The second and subsequent executions obviously find an existing Excel application even though: Excel was closed; the taskbar shows no Excel icon; the task manager shows no Excel application.

  3. #3
    Dunro is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Location
    Toronto, Canada
    Posts
    39
    Quote Originally Posted by Rod View Post
    The odd thing is that the createobject branch is taken only on the first execution. The second and subsequent executions obviously find an existing Excel application even though: Excel was closed; the taskbar shows no Excel icon; the task manager shows no Excel application.
    Do you mean the task manager lists no EXCEL.EXE under processes, or that task manager shows no Excel under Application? New instances are usually Invisible, and wouldn't show under Application.

  4. #4
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Quote Originally Posted by Dunro View Post
    Do you mean the task manager lists no EXCEL.EXE under processes, or that task manager shows no Excel under Application? New instances are usually Invisible, and wouldn't show under Application.
    Gosh, that was a year ago! If memory serves me correctly then there was no Excel application in Task Manager. I'm not sure if I looked under the processes.

  5. #5
    Dunro is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Location
    Toronto, Canada
    Posts
    39
    Oops, didn't mean to resurrect an old thread... Clearly got my 2012s and 2013s mixed up. :-)

  6. #6
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Don't worry about that; this is far more interesting than the usual run-of-the-mill, "How do I do cascading combo boxes?" It's obvious that, true to the concepts of OOP, once Access has created a reference to an Excel object then that Excel object is not destroyed as long as the Access reference is itself current. I wish this was true across all Ofice Automation products but I've experienced Access references 'left high and dry' after the target Office Automation application is closed. The most secure way that I have found is to test each time I use a cross-application reference to see whether that reference is still valid.

  7. #7
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Wow - thanks for resurrecting this - I'm doing Access-to-Excel design at the moment, and this is another item to keep in mind...

  8. #8
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Dal,

    Even if you follow something akin to post #2 (remember to reset error handling) there are certain Excel methods that I cannot get to work when issued from Access but that work fautlessly when issued from an Excel session. Sorry to be somewhat vague but I can't remember offhand what they are except that they were multisheet operations.

    Rod

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Bacode Applications.
    By cap.zadi in forum Reports
    Replies: 10
    Last Post: 12-28-2011, 11:34 AM
  2. Applications.CreateControl
    By msr71 in forum Programming
    Replies: 5
    Last Post: 04-24-2011, 09:01 PM
  3. How to use one DB file in many other DB applications ?
    By cement in forum Database Design
    Replies: 4
    Last Post: 01-01-2011, 05:50 AM
  4. Killing open applications using VBA
    By zippy483 in forum Programming
    Replies: 4
    Last Post: 03-03-2010, 10:04 PM
  5. Front ends apps in split applications
    By Coolpapabell in forum Access
    Replies: 4
    Last Post: 10-14-2009, 01:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums