1

I am editing a VBA macro that has worked for Office 2003, 2007, 2010, 2013 etc, on any previous OS version of windows we've used. Now that we've upgraded to windows 10, this line of code:

Private m_document As MSXML2.DOMDocument

Doesn't work. It needs to be

Private m_document As MSXML2.DOMDocument60

In context, I am declaring explicit global variables, and this is one of them.

Problem is, I need to edit these macros on my computer, but they need to also work on older OS versions because the product is still used on those versions. I have tried a bunch of stuff to figure out if there's any way to make this work otherwise, no dice. MSXML6 is on both my old (running windows 7) and new computer. So at this point, my hope is for there to be a way to define this variable depending on which OS is being used. I know you can check the OS, I've found code for that, but is there a way to conditionally define the variable based on the OS?

ie:

if windows10
  Private m_document As MSXML2.DOMDocument60
else
  Private m_document As MSXML2.DOMDocument 
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • Mmm. MSXML 6 parser was/is distributed with every version of Office beginning with 2003, as I recall, because 2003 was the version XML features were introduced. Are you sure it's not available on those other systems and that simply changing the declarations from DOMDocument to DOMDocument60 doesn't work? – Cindy Meister Apr 01 '16 at 12:14
  • One of the first things I tried, because of exactly what you said. I get a Type Mismatch error when I change to DOMDocument60. – user2658277 Apr 01 '16 at 15:03

1 Answers1

1

You need to use late binding.

 Private m_document As Object

 Sub testSub()

     If windows10 Then
          Set m_document = CreateObject("MSXML2.DOMDocument60")
     Else
          Set m_document = CreateObject("MSXML2.DOMDocument")
     End If

 End Sub

As a bonus tip, if you do this, you will lose the intellisense in VBA. So, you can set it as "Private m_document As MSXML2.DOMDocument60" and create all of your code with intellisense and then once you are ready to deploy, change back to late binding as above.

OpiesDad
  • 3,385
  • 2
  • 16
  • 31
  • So...this didn't completely work either, not because it's wrong, but because CreateObject("MSXML2.DOMDocument60") doesn't work correctly. Oddly enough, CreateObject("MSXML2.DOMDocument") worked fine on both platforms. So thank you, this helped, I don't know why it is working like this, and I think I just hate VBA a little more than I did before – user2658277 Apr 01 '16 at 16:15