Results 1 to 24 of 24

Thread: VBA run-time automation error

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Atlanta, GA
    Posts
    9

    Question VBA run-time automation error

    Hi - I have a macro that is supposed to open a page on a web site, select and copy the entire contents of that page, then paste into a new workbook. It works properly the first or second time I run Excel after a system restart. After that I receive an error stating: Run-Time Error '-2147417848 (80010108)': Automation error. The object invoked has disconnected from its clients. I uploaded and attached a screen shot of this error window. Once it occurs the first time, I get this error every other time on the connection .REFRESH command until I close down and restart my system. I'm wracking my brain trying to figure out why it works one or two times then stops. I've tried clearing IE cache and changing the IE page view setting to refresh the page on every visit thinking therre may be a link between how IE works the web and how my VBA code is interacting with the web, to no avail. I've searched Microsoft knowledgebase for this error and see references to it, but nothing seems related to getting this error when using the statement With Worksheets(1).QueryTables.Add(Connection:=... Does anyone have a clue what might be happening, or what else I can try to get around this problem? Thanks.
    Attached Images Attached Images  

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

    Re: VBA run-time automation error

    Welcome to the Forums.

    Can you post your Excel related code?
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Atlanta, GA
    Posts
    9

    Re: VBA run-time automation error

    Here's the beginning of the macro...
    Sub CreateNewAliasTable()
    '
    ' CreateNewAliasTable Macro
    ' Macro recorded 3/24/2005 by DZugel
    '
    Dim i, j, k, l As Integer
    Dim iHeadRow, iSplitRow, iLastRow, iLastCol As Integer
    Dim iFirstRow, iSecondRow As Integer
    Dim sPath, sSubPath, sHostname, sFile, sVanstat, sTemp As String
    '
    sPath = Application.DefaultFilePath
    sSubPath = "Projects\Patrol"
    sHostMaster = "Host Code Master"
    sFile = sPath & "\" & sSubPath & "\" & sHostMaster & Format(Date, " yyyy mmdd") & ".xls"
    sVanstat = "URL;http//techweb.xxxx.com/team/abc/abcstat.htm"
    Workbooks.Add
    With Worksheets(1).QueryTables.Add(Connection:=sAbcstat, Destination:=Range("Sheet1!A1"))
    .Name = "abcstat"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=True
    End With
    Sheets("Sheet1").Name = "HostAliasTable"

    I assign the macro to an icon on my toolbar. As you can see, its only the third statement after the declarations and initialization code. There is a lot more code following this, and will include it if needed, but it mainly deals with formatting the imported data. The .Refresh BackgroundQuery:=True statement is where the system fails when I step through.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Atlanta, GA
    Posts
    9

    Re: VBA run-time automation error

    One more thing, the basic structure of this code came from the Record macro feature within Excel itself.

  5. #5
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Re: VBA run-time automation error

    try setting it to false...see if works...that's how i do it...

    The .Refresh BackgroundQuery:=True

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Atlanta, GA
    Posts
    9

    Re: VBA run-time automation error

    Good morning - I tried setting the code to:
    .Refresh BackgroundQuery:=False
    with no different results. I immediately get the same error window. After the first time, though, I get a Run-time error '1004' (see attached). Can anything further down in the code file be causing these errors? This is a tough one. Thanks for any further help.
    Attached Images Attached Images  

  7. #7
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: VBA run-time automation error

    Your first post suggests that the connection object you are using is closed by something at some point.

    Error 1004 is a nonspecific error and is not very useful (to you or me).
    If background query is now erroring in your code, then change it back to true.

    See if the connection object is still open before refreshing? (debug mode ... breakpoint in the code)

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Atlanta, GA
    Posts
    9

    Re: VBA run-time automation error

    OK, I added some DEBUG.PRINT code after reworking the connection statement. It now looks like this:
    With Worksheets(1).QueryTables.Add(Connection:=sVanstat, Destination:=Range("Sheet1!A1"))
    .Name = "vanstat"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    End With
    Debug.Print Worksheets(1).QueryTables("vanstat").Name
    Debug.Print Worksheets(1).QueryTables("vanstat").Connection
    Debug.Print Worksheets(1).QueryTables("vanstat").MaintainConnection
    Debug.Print Worksheets(1).QueryTables("vanstat").QueryType
    Debug.Print Worksheets(1).QueryTables("vanstat").WebSelectionType
    Debug.Print Worksheets(1).QueryTables("vanstat").SourceDataFile
    With Worksheets(1).QueryTables("vanstat")
    .Refresh BackgroundQuery:=False
    End With
    Sheets("Sheet1").Name = "HostAliasTable"
    The strange thing is that it now works most of the time. But once a problem develops I must shut down Excel and restart before I can try to run the macro again. Does Excel keep a connection open if this kind of error is found during processing so that the macro cannot be rerun until Excel is closed? Any idea why it seems to be working now even though the only changes to the macro are the DEBUG.PRINT lines and moving the REFRESH statement to a separate WITH statement? Confused, but not so frustrated, in Atlanta.

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Atlanta, GA
    Posts
    9

    Re: VBA run-time automation error

    I found something else out that is even more strange. Right after I sent the last post, I turned off all debugging and the Immediate Window and tried to run the macro again. It immediately failed. I shut down IE and Excel, restarted Excel, tried again and it still failed. So I reactivated Breakpoints at the Workbook.ADD and the first DEBUG.Print statements, started the macro, hit F8 to step through the code, and voila - it worked again. I reran it then 4 or 5 times, each time stepping through the code, and it worked. Can this error be a timing problem of some kind since it seems to work when I step through the code but can't get it to work when I take all debugging out? Still unsure what to try next.

  10. #10
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: VBA run-time automation error

    Before you do the refresh - try puting one or two DoEvents (this hands windoze the control for a turn) which might help.

    Also, when you say it errors without the breakpoint mode, can you pinpoint where it errors?
    Do you use error handling (ignoring?) ?
    If you leave the debug.print bits in but don't use the breakpoints, does it still crash?

    Perhaps closing off any connections would help?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Atlanta, GA
    Posts
    9

    Re: VBA run-time automation error

    When it errors it is always on the .Refresh BackGroundQuery=False statement.

    I tried keeping the DEBUG.Print statements in with no breakpoints and it still errors.

    I do not use any error handling.

    How do I close a connection? I have no other connections open except this one accessing a URL.

    I added first one, then two, DoEvents statements and the macro still errors. I put one DoEvents just before my DEBUG.Print statements and one just after them, but before the .Refresh statement.

  12. #12
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Re: VBA run-time automation error

    ! BACKUP YOUR STUFF ! before you try my stuff...this may sound stupid, but look in the name box..i have encountered that add(connection adds names...each time...it stuffs it all up...if possible try your macro on a new worksheet(or QueryTable.Delete)...see if any difference....mine imports from local files this is what i use each day 12,000 times daily and it is as faster than "Workbooks.OpenText" if i remember right i use the 'on error resume next because the local file may not exist... now from online should not matter
    AND BE CAREFUL as this is specific to my sheets...I don't use names or labels...SO BACKUP YOUR STUFF!!!!!!
    VB Code:
    1. 'IMPORT TEXT FILE DATA
    2.     'TURN OFF LABELS TO PREVENT PROBLEMS WITH THIS FUNCTION
    3.     'THEN TURN ON IMMEDIATELY AFTER THIS FUNCTION
    4.     'THIS WORKBOOK USES NO LABELS SO NO PROBLEM
    5.     ActiveWorkbook.AcceptLabelsInFormulas = False
    6.    
    7.     FILEADDRESS = "TEXT;C:\StockBook\TDB\130xD\" & THISSTOCK & ".txt"
    8.     On Error Resume Next    'ERRORS WILL SHOW UP IN DATE MATCHING
    9.     With ActiveSheet.QueryTables.Add(Connection:= _
    10.         FILEADDRESS, Destination:=Range("A1137"))
    11.         .Name = ""
    12.         .FieldNames = False
    13.         .RowNumbers = False
    14.         .FillAdjacentFormulas = False
    15.         .PreserveFormatting = True
    16.         .RefreshOnFileOpen = False
    17.         .RefreshStyle = xlOverwriteCells
    18.         .SavePassword = False
    19.         .SaveData = False
    20.         .AdjustColumnWidth = False
    21.         .RefreshPeriod = 0
    22.         .TextFilePromptOnRefresh = False
    23.         .TextFilePlatform = xlWindows
    24.         .TextFileStartRow = 1
    25.         .TextFileParseType = xlDelimited
    26.         .TextFileTextQualifier = xlTextQualifierNone
    27.         .TextFileConsecutiveDelimiter = True
    28.         .TextFileTabDelimiter = False
    29.         .TextFileSemicolonDelimiter = False
    30.         .TextFileCommaDelimiter = True
    31.         .TextFileSpaceDelimiter = False 'CHANGE THIS FOR NOW TO FALSE (3/13/05)
    32.         .Refresh BackgroundQuery:=False
    33.     End With
    34.     ActiveWorkbook.AcceptLabelsInFormulas = True
    35.     Cells.Select
    36.     Selection.QueryTable.Delete
    37.     QnamesDelete
    VB Code:
    1. Sub QnamesDelete()
    2.     Dim NM As Name
    3.     For Each NM In ActiveWorkbook.Names
    4.     NM.Delete
    5.     Next
    6. End Sub
    Last edited by MJBNET; Apr 12th, 2005 at 10:35 PM. Reason: forgot a bit..

  13. #13
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Re: VBA run-time automation error

    Quote Originally Posted by dzugel
    The strange thing is that it now works most of the time. But once a problem develops I must shut down Excel and restart before I can try to run the macro again. Does Excel keep a connection open if this kind of error is found during processing so that the macro cannot be rerun until Excel is closed? Any idea why it seems to be working now even though the only changes to the macro are the DEBUG.PRINT lines and moving the REFRESH statement to a separate WITH statement? Confused, but not so frustrated, in Atlanta.
    i think you might have answered yourself here...excel's thinking is your friend or enemy...you will encounter this with formattimg in cells...like date stuff...here BACKUP YOUR WORK....THEN [QueryTable.Delete] or try your macro on a new sheet , workbook, whatever... i think by hand you select all cells then delete...you get a message if ya wanna bla bla...kill it all... now retry.. lemme know...there is now doubt there is a way to to get yur stuff on yur sheet

  14. #14

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Atlanta, GA
    Posts
    9

    Re: VBA run-time automation error

    There are no NAMES defined prior to beginning this macro, and no Workbooks are open. This macro creates a new Workbook then tries to open a single HTML document on an internal web site using a URL connection. There are no other sheets or workbooks open and I start the macro immediately after I begin Excel. There is no formatting in the sheet. I tried the QueryTable.Delete command with no success. I keep thinking there is something wrong with the URL connection, but it at least appears to work. As I step through the code and step into the REFRESH command, I see the system connect to the page. Then it usually fails, but will work properly sometimes and retrieve the entire page as needed. I think it's about time to stop trying to completely automate this process and just manually retrieve the data, paste it into the sheet, and have the macro to the necessary formatting. Unless there is something else to try.

  15. #15

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Atlanta, GA
    Posts
    9

    Resolved Re: VBA run-time automation error

    Thanks to all who offered ideas and assistance while I worked on this problem. It never was resolved and continues today. I'm giving up trying to fix the automatation and will handle this task manually. Case closed!

  16. #16
    New Member
    Join Date
    Jan 2007
    Posts
    2

    Re: VBA run-time automation error

    Hi,

    I'm new to this thread but I had a similar problem and believe I've figured out the problem.

    My process allowed me to change the selection criterions before it run. However, one of the values selected was defined as a global variable and was modified during the process. In my case it was a date value which was selected out of the database in 'mm/dd/yyyy' format and I needed to do a comparison on a 'dd/mon/yy' date. I converted the input data to 'dd/mon/yy' and this worked fine the first time, however, the next time it ran the new format was maintained and caused the conversion to the new format to fail.

    I tracked down my problem by displaying the sql before it was processed.
    I added....

    Sheets("BASE_DATA").Select
    Worksheets("BASE_DATA").Activate
    Range("A3501").Select
    ActiveCell.FormulaR1C1 = "" & sql


    to the front of my 'run_sql' routine. When the 'Refresh BackgroundQuery:=False' was returned I was then able to see the sql causing the problem and work my way back with the de-bugger tool.

    I hope this helps.

  17. #17
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: VBA run-time automation error

    Hey way to go bumping something almost a year old. I managed to read it some before I realized how old it was. Looks like the problem could have been solved by turning Option Explicit on. He assigned a connection like so..

    VB Code:
    1. Connection:=sAbcstat

    but that variable was never declared or assigned. He later changed it to ...

    VB Code:
    1. Connection:=sVanstat

    and said it started working and errored somewhere else. That's just before he gave up. This could have been fixed. I wonder if he's still around.

  18. #18
    New Member
    Join Date
    Jan 2007
    Posts
    2

    Re: VBA run-time automation error

    Yes, it is an old call, however it helped me to get an idea of what to look for and I saw that dzugel had given up so I thought he/she might still be out there doing the process manually. If not I'm sure someone else will come across this problem.

  19. #19
    New Member
    Join Date
    Jul 2007
    Posts
    3

    Re: VBA run-time automation error

    I came across the same error in VBA, and I was just trying to refresh a querytable in my ThisWorkbook object Workbook_Open event. Thanks to you all, all I had to do was add "Option Explicit" at the top and for some reason the error went away. For today, anyway. I hope it doesn't pop up again at some point.
    It may also have something to do with the backgroundquery T/F thing.

  20. #20
    New Member
    Join Date
    Jun 2008
    Posts
    1

    Re: VBA run-time automation error

    Hi, even after all these years.....

    I am getting the same error and cannot figure out the problem. If I use F8 to step through my code, I can just hold it down and let it run as fast as it can, it works, but as soon as I click run, it errors out with the run-time error and locks up Excel and I have to kill it through Task Manager. I tried the Option Explict, that didn't seem to work.

    Any other ideas?

  21. #21
    New Member
    Join Date
    Jul 2007
    Posts
    3

    Post Re: VBA run-time automation error

    I thought that "option explicit" helped, but I have continued to run in to this problem over the past year. I am posting my final workaround (not solution, really) here:

    This code goes in the "ThisWorkbook" object on the workbook:
    Code:
    Private Sub Workbook_Open()
        'Delay it for late binding??
        Application.OnTime Now + TimeValue("00:00:03"), "StartupRefresh"
    End Sub
    Put this code in a module in the workbook:
    Code:
    Sub StartupRefresh()
         Dim IsSaved As Boolean
    
         IsSaved = ThisWorkbook.Saved
      'I get the error when I try to do this.  You may be trying to do something different...
         ThisWorkbook.Sheets("DBQuery-Workdays").QueryTables(1).Refresh
         If IsSaved Then ThisWorkbook.Saved = True
    
    End Sub
    Basically, I am scheduling a delayed run of whatever I'm trying to do when I get the automation error.

    Let me know if it works for you!

  22. #22
    New Member
    Join Date
    Feb 2010
    Posts
    1

    Re: VBA run-time automation error

    jeng02,
    Thank you for the post. I had the same problem. it appears the connection requires a nano second or two, and the latency provided by using the statement and the sub() below solves the issue.

    Application.OnTime Now + TimeValue("00:00:03"), "RefreshTable(qt)"

    Sub RefreshTable(qt As QueryTable)
    qt.Refresh
    End Sub


    Thank you sincerly,
    tgla

  23. #23
    New Member
    Join Date
    Jul 2007
    Posts
    3

    Smile Re: VBA run-time automation error

    I'm glad it worked for you. I've been helped so many times on forums like these, it's nice to be able to help someone else for a change!

  24. #24
    New Member
    Join Date
    Jan 2016
    Posts
    1

    Re: VBA run-time automation error

    Hey! Huge thanks to you all, I had the same problem, and your ideas helped me to find the solution, which is really works for me!

    After finishing With block add DoEvents

    it should be smth like this:

    With

    '............
    'your code with adding and refreshing table
    '...........
    End With
    DoEvents


    Know that this topic is too old, but maybe it also helps someone

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