Load second vba project file or reference subroutine in other project file

Load second vba project file or reference subroutine in other project file

pball
Mentor Mentor
2,137 Views
8 Replies
Message 1 of 9

Load second vba project file or reference subroutine in other project file

pball
Mentor
Mentor

I'm looking to use two vba project files. I'll have my main ivb file and there is a second ivb file with a single script in it. I do not want to copy that script to the other project file. Is there a way to load or link the second project file from the main one?

Another thing is I've manually loaded a second project file for testing and I can run scripts from it via the Macros option on the Tools ribbon bar but I cannot add them to the ribbon bar under the customize user commands option. If there isn't a way to get scripts from a second project file added on the ribbon bar I don't mind creating a script in the main project which just runs the script from the secondary project file.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
0 Likes
2,138 Views
8 Replies
Replies (8)
Message 2 of 9

humbertogo
Advocate
Advocate
added a Reference to it in the calling Project
0 Likes
Message 3 of 9

pball
Mentor
Mentor
Would you have an example for doing that? Haven't had anything pop up in searches yet, other wise it'll be back to trying slightly difference searches later.
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
0 Likes
Message 4 of 9

humbertogo
Advocate
Advocate

You can load the 2 project and go to tools references

 

0 Likes
Message 5 of 9

pball
Mentor
Mentor
I'm getting an error that says "Automation error, Unspecified error" when I try adding the 2nd project as a reference and it won't add the reference. Well I didn't really want to but I guess I'll just copy that one script to my project until I can figure something out.
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
0 Likes
Message 6 of 9

Scott_Stubbington
Advocate
Advocate

Hello,

I've quickly created this code....

Public Sub LoadVBAProject()
LoadAnotherVBAProject "Put Location of VBA project here"
End Sub

Public Sub LoadAnotherVBAProject(pvarLocation As String)
' counter to work out the new project number
Dim varVBACounter As Long
varVBACounter = ThisApplication.VBAProjects.Count
' Open the other project
ThisApplication.VBAProjects.Open pvarLocation
' get a reference to the just opened VBA project
Dim varTempProject As InventorVBAProject
Set varTempProject = ThisApplication.VBAProjects(varVBACounter + 1)
' references for all the different parts of the VBA project
Dim varVBAComponent As InventorVBAComponent
Dim varVBAMember As InventorVBAMember
Dim varVBAArgument As InventorVBAArgument
' go through all the components of the Project and display them
For Each varVBAComponent In varTempProject.InventorVBAComponents
' print to the immediate window the name of the component
Debug.Print varVBAComponent.Name
For Each varVBAMember In varVBAComponent.InventorVBAMembers
' print to the immediate window the name of the members of the component
Debug.Print " " & varVBAMember.Name
For Each varVBAArgument In varVBAMember.Arguments
Debug.Print " " & varVBAArgument.Name & "----" & varVBAArgument.ArgumentType
Next varVBAArgument
Next varVBAMember
Next varVBAComponent
End Sub

The bit I've forgotten is how to run a Sub in a different project. Smiley Frustrated

 

HTH

 

Scott

0 Likes
Message 7 of 9

Scott_Stubbington
Advocate
Advocate

Ah pants, sorry folks, I didn't look at the date. Smiley Sad

0 Likes
Message 8 of 9

Bert_Bimmel
Advocate
Advocate

Why do you apologize for answering an old post, that hasn't been satisfyingly answered yet?

Kept me busy too, so here is what I have figured out:

 

Your proposed method works, but i find it somewhat laborious to pass arguments and results. Working with references to other projects is much "smoother" - if there wasn't a big catch:

You can create a new Project, and then put a reference to it in your Default.ivb, and everything works fine until you save everything, shut down Inventor and restart it the next day: The reference in your Default.ivb is broken! (funny detail: if you consult the broken reference's "IsBroken"-property, it does not reply "true", but causes an error instead - It's just like asking someone if he is asleep: the answer is either "no" or there's no answer).

That is because Inventor's VBA-client ins't smart enough to load referenced projects automatically. The referenced project must be loaded prior to the referencing project if the references shall still work (btw: Excel is smart enough to behave as one would reasonably expect).

Thus, you cannot have your Default.ivb reproducably reference a second project for easy passing of arguments and results, but you can create an - admittedly somewhat complicated - workaround, where you can youse your Default.ivb to reproducably load two or more projects in the correct order, and then use them with references.

 

I have attached some project's where this is demonstrated with some simple functions. It also shows how to pass arguments and receive results when not working with references. (Your post didn't make that very clear, sorry)

 

0 Likes
Message 9 of 9

mgrenier2
Collaborator
Collaborator

I'm in the exact same boat here, I'm trying to have more than one project load when Inventor starts up, at this point I really don't care what kind of project it loads, Application, Document or User, as long as it load on Inventor startup

0 Likes