+ Reply to Thread
Results 1 to 10 of 10

workbooks.open fails with no error

  1. #1
    Registered User
    Join Date
    01-20-2013
    Location
    norwalk, ct
    MS-Off Ver
    2010
    Posts
    6

    workbooks.open fails with no error

    running excel 2010 on windows 7. trying to open Book2.xlsx from Book1.xlsm. this hard-coded macro works:

    Please Login or Register  to view this content.

    This function doesn't (no error on the workbooks.open line, but wb throws object variable or with block variable not set. Passing "c:\users\steve\Book2.xlsx" as FileToOpen param

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 01-27-2013 at 01:37 AM. Reason: Added code tags, as per forum rules. Don't forget!

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: workbooks.open fails with no error

    How are you using this function, exactly?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    01-20-2013
    Location
    norwalk, ct
    MS-Off Ver
    2010
    Posts
    6

    Re: workbooks.open fails with no error

    function will be used to open different workbooks based on user input. parameter FileToOpen will be built elsewhere and passed in. Other UDFs wil act upon contents of opened workbook.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: workbooks.open fails with no error

    Which line in the code errors?

    What happens if you remove On Error Goto ErrTrap?
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    01-20-2013
    Location
    norwalk, ct
    MS-Off Ver
    2010
    Posts
    6

    Re: workbooks.open fails with no error

    the line after set wb = workbooks.open, where I debug.print wb.name - I get the object variable or with block variable #91. I'll try w/o the on error, but usually like to handle errors in the code.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: workbooks.open fails with no error

    The On Error could actually be hiding an error.

    By the way, you've not said where/how you are using this code.

    Are you calling it from another sub/function?

  7. #7
    Registered User
    Join Date
    01-20-2013
    Location
    norwalk, ct
    MS-Off Ver
    2010
    Posts
    6

    Re: workbooks.open fails with no error

    right now, I'm calling the funtion from a cell in Book1.xlsm, =OpenWorkbook1(B2), where cell B2 contains the string "f:\Book2.xlsx". Ultimately, I want the code in an add-in to be able to use in multiple spreadsheets. I took the on error out, and no runtime error is generated (unless I reference a property of wb, which was dim'ed as a workbook.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: workbooks.open fails with no error

    That's the problem - calling the function from a cell.

    User defined functions on worksheets return values to the cell they are in, they don't carry
    out other operations like opening files.

  9. #9
    Registered User
    Join Date
    01-20-2013
    Location
    norwalk, ct
    MS-Off Ver
    2010
    Posts
    6

    Re: workbooks.open fails with no error

    the function does return a string (value indicates if success or failure to open the workbook). can a sub procedure carry out other operations, ie - opening workbooks?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: workbooks.open fails with no error

    The problem is that you are using the function on a worksheet.

    If you called this function from a sub or other function then there'd be no problem.

    For example if I run this code I get a message box with the path and filename, and the file is opened.
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1