0
votes

I have Excel VBA code that relies on the Microsoft Office Project Object Library 16.0 to run. It executes perfectly when the library is manually selected (by going to Tools -> References and checking it off). I don't want my users to have to select the project library - in fact I'd rather they stay away from the VBA editors completely - so I tried adding it inline using its GUID.

For the library GUID, I found {A7107640-94DF-1068-855E-00DD01075445} with Major and Minor numbers 4 and 9, respectively. I then tried running the following without the library selected:

Sub testReference()
ThisWorkbook.VBProject.References.AddFromGuid GUID:="{A7107640-94DF-1068- 
855E-00DD01075445}", Major:=4, Minor:=9
Dim MP_App As MSProject.Application
Set MP_App = CreateObject("Msproject.Application")
Dim MP_Project As MSProject.Project
MP_App.Visible = True
MP_App.FileOpenEx Name:="C:\Users\Adam Wertheimer\Desktop\testProjectFile.mpp"
End Sub

except it flags the line

Dim MP_App As MSProject.Application

with the compile error "User-defined type not defined", so either the library didn't get added properly or there is something else I'm missing. I really don't want to have all users have to check off the project library manually, but if I have to then so be it. Any advice/help would be greatly appreciated.

1
Can't you add the library through the VBE's Tools, References?user4039065
@Jeeped, yes I personally did that and my code runs fine with it manually selected. The issue is that I may have many people using my macro (who have varying amounts of VBA knowledge) and I don't want to force each of them to have to manually select the library on their machines.Adam Wertheimer
If you're sure you've added the library in correctly just use Dim MP_App As object for late binding. The CreateObject will take care of the rest. If you wanted to add a late-bound dictionary class, you wouldn't import the library, just dim as object and createobject.user4039065
@Jeeped yep, you're right. I imported the library properly (as I thought) and so the "MSProject.Application" and "MSProject.Project" object types are to be replaced by "Object". Thank you!Adam Wertheimer
This is really a duplicate question. From what you have written, there may be some steps you have missed. See How to add a reference programmatically. In your case, you are probably better off using late binding.Ron Rosenfeld

1 Answers

0
votes

Importing the library reference by means of the associated clsid guid does not grant immediate access to all faculties. You may need to obscure some direct references.

Dim MP_App As object
Dim MP_Project As object

By declaring the Microsoft Office Project specific vars as vanilla objects, you should be able to instantiate a var that is ready to accept the late bound CreateObject. Once the MP_App has been created, the MP_Project should be able to be set as one of its members.