Home Excel VBA: Workbooks.Open returns the wrong object
Reply: 0

Excel VBA: Workbooks.Open returns the wrong object

user930
1#
user930 Published in April 25, 2018, 8:21 pm

In Excel 2016 (don't remember it happening in earlier versions), I'm beginning to encounter issues where the Workbooks.Open command does not return the correct Workbook object. For example:

Dim wbkHelper as Workbook
Set wbkHelper = Workbooks.Open(filePath, ReadOnly:=true)

The Open command functions correctly - it opens the "filePath" file like it's supposed to - but wbkHelper ends up pointing to the completely wrong file, typically the ThisWorkbook object. The wbkHelper object is never reassigned to after that point in the code, so it's not something the code is doing. This only happens sometimes and can be difficult to reproduce, but it breaks the entire macro when it does happen.

I can think of numerous ways to work around this, but it shouldn't be necessary? This is a core VBA function that's used all the time, and it's something you expect to just work.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305753 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO