Results 1 to 3 of 3

Thread: Automation Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Automation Error

    I know there are numerous threads on the forums about this error, but so far none have held the solutions.

    I am getting this error:

    Automation error
    The object invoked has disconnected from its client

    when trying to copy a worksheet to another book. Here is my code
    VB Code:
    1. Public xlApp As Object 'Excel.Application
    2. Dim xlBook As Object 'Excel.Workbook
    3. Dim xlSheet As Object 'Excel.Worksheet
    4.  
    5. 'Constructor
    6. Public Sub Class_Initialize()
    7.     Set xlApp = CreateObject("Excel.Application")
    8.     xlApp.displayalerts = False
    9.     Exit Sub
    10. End Sub
    11.  
    12. Public Function OpenFile(FileName As String) As String
    13.     If xlApp.activeworkbook Is Nothing Then
    14.         Set xlBook = xlApp.workbooks.open(FileName)
    15.     Else
    16.         If Not xlApp.activeworkbook.fullname = FileName Then
    17.             xlApp.activeworkbook.Close savechanges:=False
    18.             Set xlBook = xlApp.workbooks.open(FileName)
    19.         End If
    20.     End If
    21.     xlApp.worksheets(1).Activate
    22.     Set xlSheet = xlApp.activesheet
    23.     OpenFile = xlSheet.Name
    24. End Function
    25.  
    26. Public Sub MergeBooks(Dest As String, Source As String)
    27.     Dim wbDest As Object
    28.     Dim wbSource As Object
    29.     Set wbDest = xlApp.activeworkbook
    30.     OpenFile (Dest) 'will close any open books
    31.     Set wbSource = xlApp.workbooks.open(Source)
    32.  
    33.     For Each xlSheet In wbSource.worksheets
    34.         If xlSheet.Name <> "Info" Then
    35.             'The Automation error occurs here
    36.             xlSheet.Copy after:=wbDest.worksheets(wbDest.worksheets.Count)
    37.         End If
    38.     Next xlSheet
    39.    
    40.     '(TODO) sheet 1("info") in both books contain a list, they need adding
    41. End Sub

    Please help

  2. #2

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Re: Automation Error

    I have spent the last 2 hours reading through post after post trying to find a solution to this, only to find what I am looking for within my own post.

    After reading through my post, I noticed that
    Set wbDest = xlApp.activeworkbook
    OpenFile (Dest) 'will close any open books
    should infact be
    OpenFile (Dest) 'will close any open books
    Set wbDest = xlApp.activeworkbook

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Automation Error

    You shouldnt use "activeworkbook" or" activesheet" as they can cause issues from actions like this. Just use the name of your workbook or sheet instead.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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