|
-
Mar 9th, 2006, 02:17 AM
#1
Thread Starter
New Member
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:
Public xlApp As Object 'Excel.Application
Dim xlBook As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
'Constructor
Public Sub Class_Initialize()
Set xlApp = CreateObject("Excel.Application")
xlApp.displayalerts = False
Exit Sub
End Sub
Public Function OpenFile(FileName As String) As String
If xlApp.activeworkbook Is Nothing Then
Set xlBook = xlApp.workbooks.open(FileName)
Else
If Not xlApp.activeworkbook.fullname = FileName Then
xlApp.activeworkbook.Close savechanges:=False
Set xlBook = xlApp.workbooks.open(FileName)
End If
End If
xlApp.worksheets(1).Activate
Set xlSheet = xlApp.activesheet
OpenFile = xlSheet.Name
End Function
Public Sub MergeBooks(Dest As String, Source As String)
Dim wbDest As Object
Dim wbSource As Object
Set wbDest = xlApp.activeworkbook
OpenFile (Dest) 'will close any open books
Set wbSource = xlApp.workbooks.open(Source)
For Each xlSheet In wbSource.worksheets
If xlSheet.Name <> "Info" Then
'The Automation error occurs here
xlSheet.Copy after:=wbDest.worksheets(wbDest.worksheets.Count)
End If
Next xlSheet
'(TODO) sheet 1("info") in both books contain a list, they need adding
End Sub
Please help
-
Mar 9th, 2006, 02:26 AM
#2
Thread Starter
New Member
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
-
Mar 9th, 2006, 02:50 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|