2

I have a module that looks up a website and does a bunch of things. In cleaning up the code, I added the Option Explicit tag, and it stop compiling.

It's running into trouble on the following line

dim objHTML As htmldocument

I tracked down the issue, and it was because I am on a different computer, and the Microsoft HTML Object Library reference was not turned on.

So the question is, how do I turn on the Microsoft HTML Object Library reference on using vba in such a way that I can still compile use option explicit?

4
  • 2
    In the VBE go to Tools ► References then locate Microsoft HTML Object Library and put a checkmark beside it. Commented Jul 2, 2016 at 20:25
  • I can do that easily enough. But some people using this spreadsheet will not have the knowledge to do that. So I want to be able to turn it on for them using VBA, but when I try, it stops compiling because of the option explicit Commented Jul 2, 2016 at 20:26
  • 1
    It is carried with the VBA project to other computers. However, I will post the late binding solution below. Commented Jul 2, 2016 at 20:27
  • Ah, so it wasn't because I'm on a new computer, but because I started a new version of the spreadsheet to clean everything up and I hadn't turned it on? Although late binding will be helpful too. Thank you for your time Commented Jul 2, 2016 at 20:29

1 Answer 1

2

This is known as 'early binding' and you must have the non-default Microsoft HTML Object Library reference included. Do this by getting into the VBE and go to Tools ► References then locate Microsoft HTML Object Library and put a checkmark beside it.

This reference is carried along with the VBA project and using the worksheet on another computer will not erase the reference. You probably got some code from another VBA project (or a handy site like SO) and pasted it into your new project.

However, you can use 'late binding' to use the same thing. This is done with VBA's CreateObject function to create you IE object which has its own document. You just need to know what object to create.

Dim objIE As Object, objHTML As Object

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate2 "https://stackoverflow.com/questions/38164172/option-explicit-with-htmldocument"

Do While objIE.busy Or objIE.readystate <> 4: DoEvents: Loop

Set objHTML = objIE.document
Debug.Print Left(objHTML.body.innertext, 1024)
Sign up to request clarification or add additional context in comments.

2 Comments

I normally use early binding as default. Would it be better coding practice for me to change to late binding as a default?
Never, never, never use late binding when you have a choice (and you almost always do). Late binding does not bring every property and method into the project (a Scripting.Dictionary is a prime example) and if nothing else, you have to perform your own capitalization when writing the code. Early binding runs faster (it is preloaded) and provides more methods and properties. I only use late binding in code examples because I'm too lazy to have to rewrite the same paragraphs explaining to the OP what references to include and why over and over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.