Results 1 to 5 of 5

Thread: EXCEL Macros quit working: My Set EXCELWorkBook = Statement fails

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2018
    Posts
    25

    EXCEL Macros quit working: My Set EXCELWorkBook = Statement fails

    Windows 7, MS Office 2007:
    Recently the EXCEL Macros I've been using for years quit working. I defined the working Objects as Globals:
    Global gobjEXCEL As Object
    Global gobjEXCELWorkBook As Object
    Global gobjEXCELWorksheet As Object

    Then I use SET Statements to name the EXCEL Objects I want to use:
    1) Set gobjEXCEL = CreateObject("Excel.Application")
    2) Set gobjEXCELWorkBook = GetObject(pzExcelFileName)
    3) Set gobjEXCELWorksheet = gobjEXCELWorkBook.Worksheets("Sheet1")

    1 works, gobjEXCEL= Microsfoft Excel
    2 fails, without setting ERR, subsequently 3 fails

    This code has worked for a long time and only stopped working i n the last few days after a flurry of MS Updates.

    Anybody know whats wrong with my Globals and/or Set Statements? I recently seemed to have a related problem ( Updates??? ) with VB6 and Access which got resolved with some Reference changes. But I haven't found a clue in the Object Browser and there aren't any Reference choices in EXCEL!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: EXCEL Macros quit working: My Set EXCELWorkBook = Statement fails

    1) Set gobjEXCEL = CreateObject("Excel.Application")
    2) Set gobjEXCELWorkBook = GetObject(pzExcelFileName)
    3) Set gobjEXCELWorksheet = gobjEXCELWorkBook.Worksheets("Sheet1")
    using getobject may open the workbook in a different instance of excel to your application object (or if the workbook is already open would be sure to be in the already open application, not the one you just created), if you need to use getobject you should create the application object from the workbook object, if you actually need the application object as well as the workbook object

    maybe try like
    Code:
    Set gobjEXCELWorkBook = GetObject(pzExcelFileName)
    Set gobjEXCEL = gobjEXCELWorkBook.application
    Set gobjEXCELWorksheet = gobjEXCELWorkBook.Worksheets("Sheet1")
    i can not say that this is causing your problem, but at least something to be aware of
    i would also look at if the path to pzexcelfilename is valid and not virtualized, as some of these can vary between updates

    if the workbook will not already be open try the workbooks.open method
    Code:
    Set gobjEXCELWorkBook = gobjEXCEL.workbooks.open(pzExcelFileName)
    i do not believe what you define your variables as would make any difference at all, the only reason to declare them as global would be to keep them valid between different procedures
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2018
    Posts
    25

    Re: EXCEL Macros quit working: My Set EXCELWorkBook = Statement fails

    westconn1: Thank you for the explanation/examples. Being on old mainframe system guy I just generally trusted the software to do something reasonable and it usually did. In this case I became a bit paranoid because I seem to have other examples of the same code that works. And, sometimes the code in the examples that don't work, even though they were modified a few weeks back, appeared to have significant, but critical, changes that I can't imagine doing! I'm still experimenting, but I'll try out your suggestions and let you know... BTW, I do generally access the retrieved data in various Functions and Subroutines so it needs to be Global. The application involved takes Spreadsheets I've copied from data providers and reformats them for conveniently stuffing them into an Access Db. So I have to unmerge rows, rearrange, and delete columns as well as remove various styles, background color, shapes, and et ceteras.

  4. #4
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: EXCEL Macros quit working: My Set EXCELWorkBook = Statement fails

    I might be off here but i never entertained the idea of opening and getting a reference through get object etc when you can open and get a reference using excels built in methods. i do remember certain situations where i needed to use getobject if an application i had was 'Looking' for an open excel application, but actually opening a new application i would never use this method.

    My point is using methods that excel provides might be a better way since they generally carry over updates and getobject which is from the good old days of vb6 might be considered stone age.

    Since your using vba from within excel (it seams), use the excel methods to open files in excel.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: EXCEL Macros quit working: My Set EXCELWorkBook = Statement fails

    also global is a bit old school, public should be enough if placed in a standard module
    also we have no idea how you are disposing of your objects when finished with them

    with VB6 and Access which got resolved with some Reference changes.
    the bits of code as posted requires no references
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

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