Declaring early bound MSXML object throws an error in VBA - excel

I am trying to import xml data into excel..
So the first line of the code is
Dim XMLDOC As MSXML2.DOMDocument
and this gives an error "user defined type not defined"

Inside the VBE, Go to Tools -> References, then Select Microsoft XML, v6.0 (or whatever your latest is. This will give you access to the XML Object Library.
Updated with fancy pic!

I had DOMDocument defined which needed Microsoft XML, v3.0 but I had Microsoft XML, v6.0 selected in references which caused the below error
"user defined type not defined".
The solution
The solution was to either change DOMDocument to DOMDocument60 (60 uses ver 6.0) or use the Microsoft XML, v3.0 reference with DomDocument.
Just a quick note, if anyone is using a different version, such as Microsoft XML, v4.0, then DOMDocument40 should be used. This is because the number at the end of the DOMDocument is specific to the version of the library being used.

I work with a VBA Excel Macro that someone else wrote and I was tasked with fixing it after recently upgrading from Windows 7 / Office 2010 to Windows 10 / Office 2016. I started to receive the same "user defined type not defined" compile error. My previous install also had MS XML v6.0 but apparently you have to specifically point to this version in your code on Windows 10 and/or Office 2016 (I wasn't able to confirm which upgrade caused the issue). I was able to resolve the issue by doing a Find/Replace on the following:
"DOMDocument" to "MSXML2.DOMDocument60"
"XMLHTTP" to "MSXML2.XMLHTTP60"

I am using Microsoft Windows 10 & Office 2016.
Using Microsoft XML 6.0 does not fix the problem.
Selecting Microsoft XML 3.0 fixed the compilation error

I had the 3rd and 6th versions installed, and the project uses the 4th one. I installed the 4th version from https://www.microsoft.com/en-us/download/details.aspx?id=15697 and this solved the problem.

Related

Microsoft MS Project XX.X Object Library Reference Missing from References List in VBA

Ultimately I want to build a MS Project file using early binding from VBA behind XLS or MPP.
In order to do that, it is my understanding that you should go to Tools--> References and select the Microsoft Project XX.X Object Library.
Unfortunately, it wasn't in the list.
The References pop-up allows you to browse, and select the reference library manually. Great! But, where do I look, and what file do I select to import this reference?
Assuming you are looking in Excel VBA References's list, it should just show up. Here's mine:
Note that I see it as Microsoft Office Project XX.X Object Library, you asked about Microsoft Project XX.X Object Library (lacking Office).
My environment is Win10; both Excel 2016 32-bit and Project 2016 32-bit are installed.
Perhaps try re-installing your Office components? Perhaps try making sure you have both the same bit architecture (32 or 64).

Read/Write Excel files Visual Studio 2017

I just installed Microsoft Office 2010 (I'm guessing this has some references the program needs), and I'm using Visual Studio Community 2017, what reference do I need to add to write and read .xslx files?
Edit:
The Microsoft Excel 14.0 reference just appeared on the COM list, but I still can't make a file. I'm guessing I need to import something now?
This assumes you actually got the reference added as you state.
So then add this at the top of the code window
Imports Microsoft.Office.Interop
and this in your code:
Dim objApp As New Excel.Application
objApp.Visible = True
https://support.microsoft.com/en-us/help/302094/how-to-automate-excel-from-visual-basic--net-to-fill-or-to-obtain-data
I'm using vb.net (Visual Studio 15) and have MS Office 16, and when I added the COM reference Microsoft Excel 16.0 Object Library to my project, it didn't take. I actually had to browse (in the add reference window) to the following location an add a dll:
C:\Windows\assembly\GAC_MSIL\microsoft.office.interop.excel\15.0.0.0_71be9bcellle9429c\microsoft.office.interop.excel.dll

EXCEL VBA: Compile error in hidden module only on MacOS

I have a problem with running a VBA in a Excel spreadsheet on a MacOS (Sierra). On Windows everything works like expected. I always get the "Compile error in hidden module: Interpolation ...
I tried to remove the whole content of the module and this is the leftover:
Function interpolate(x As Double, ran As Range) As String
interpolate1 = 1
interpolate = interpolate1
End Function
The VBA code is password protected and I don't use any ActiveX controls.
The references I use are as follows:
Visual Basic For Applications
Microsoft Excel 16.0 Object Library
OLE Automation
Microsoft Office 16.0 Object Library
Microsoft Forms 2.0 Object Library
RefEdit Control
If anyone has any suggestions on how to go about this problem, that would be great.
Thanks,
Tro
The RefEdit reference was missing on MacOS. I removed it and now it seems to work.

Missing: Microsoft PowerPoint 16.0 Object Library (Excel VBA)

I've written a piece of kit on Excel 2016 that uses a combination of formula and vba macros.
Basically some of the people that will be using this may be using an older version of Excel (2013 or 2010). When testing out whether the file will run on 2013 the Microsoft Powerpoint 16.0 library was missing and some of the code would not run.
The only fix was to add the Microsoft PowerPoint 15.0 library and then it seemed to work.
Is there any way to add the libraries automatically when I send this file to other people, or add the 15.0, 14.0 and 13.0 library's in my copy so that this is not an issue for other users?
[EDIT] From further reading it appears that older versions of excel use different libraries and it doesn't look like you can "pick and choose" whether to use 16.0, 15.0 etc. (Please correct me if I'm wrong). Apparently there is something called "Early/Late Binding" which might help me out, I assume this is referencing within my actual VBA code so if that is a viable solution any more information would be appreciated.
You have two options.
Develop on the lowest common denominating Office version (references will automatically "upgrade" on newer versions)
Change your code from early binding (using a reference to the library) to late binding (using generic Object declarations for everything related to PPT, replacing all PPT constants with their associated values). You then get a reference to Powerpoint using the CreatObject or GetObject function.

Programatically import Microsoft.Office.Interop.Excel namespace in VBA project

I'm trying to programatically import Microsoft.Office.Interop.Excel namespace into my vba project. I found this site
that displays how to import it manually, but I'm giving this project that I've been working on to people that won't know how to import it. Can anybody please help?
I assume from your question that your VBA code uses a reference to the Microsoft Excel 12.0 (or other version) library - and that your user gets an error as the reference cannot be found. In this case, you have two options:
Instead of referring to the v12 library, refer to an older library, e.g. from Excel 2003. To do so, search the net for this library and install it - or simply "relink" your file in an Office 2003 installation before shipping
Use late binding instead of early binding. In this case, you do not add a reference to the Excel library at all. Instead, you declare your objects as type Object(instead of Excel.Workbook, Excel.Application, etc.). To create a new object, instead of using Set objExcelApp = New Excel.Application, you must now use CreateObject (for new objects) or GetObject (for existing objects, e.g. an existing, running application): Set objExcelApp = CreateObject("Excel.Application"). Any later code will continue to work as usual (though it might be a tiny bit slower due to the late binding).
For further reading, check this answer.

Resources