2 of 2 people found this helpful
I am uncertain that I understands properly. What I understand is that you have created VBA Modules in PI Processbook that creates multistates objects on a display. The multistates creation is based on data provided in a spreadsheet.
You would like to add a VBA reference dynamically ( I am not sure if this is what you meant). And also dynamically insert code. And from this I understand that you would like to automate the creation of your PI Processbook files without leaving the code inside the PI Processbook displays. Is this correct?
If this is correct, I'd propose another approch than an addin, because I believe the usage of this code will be used always in the same way and does not require to be accessible after its development.
- Create a .PDI file that will serve as a template. It will contain all the code necessary to generate your displays.
- When you need to create a new display you create a copy of the template.
- Then you configure (or not) the name of the input file required. ( I would probably make the code peek the configuration file from the same directory, and keep the config file name always the same.)
- Directory example
- Directory example
- When you open PDITemplate.pdi, you click a button that lauches the display creation process based on the configuration file.
- At the end of the script, everything related to code and button will be removed.
I made a small example attached to this post. And here is the code it contains, the main logic is inside the generatePDI sub.
You will also need to adapt the RemoveReferences part, and add a line for each reference you need to remove. You may use the PrintReferences method to show the name of your currently references libraries. This is the only way I found to get the name that can be passed tp the Remove method.Warning: this is the type of code that does not forgive. Make sure you always work on a copy of your current code, because when you execute this code, your code will be deleted!
Public Sub generatePDI() ' creates all multi states objects ' ... ' remove the code RemoveCode ' remove the references RemoveReferences ' removes the button we named Button1 - button is only visible in script when enable scripting option is enabled on the object in design mode. ThisDisplay.Symbols.Remove "Button1" ' save the PDI - use save or save as as needed ThisDisplay.Save MsgBox "Display creation completed and saved" End Sub Public Sub RemoveCode() ' get the instance of the VBEditor Set Editor = Application.VBE Dim iType As Variant ' goes over all Classes, Modules or Forms in the Active VB Project For i = Editor.ActiveVBProject.VBComponents.Count To 1 Step -1 iType = Editor.ActiveVBProject.VBComponents(i).Type If iType = 1 Or iType = 2 Or iType = 3 Then ' 1=Module, 2=class module, 3=Microsoft Form, 11=active X designer, 100=document module. ' remove the Module, class or form Call Editor.ActiveVBProject.VBComponents.Remove(Editor.ActiveVBProject.VBComponents(i)) End If Next i End Sub Public Sub RemoveReferences() Set ReferencesObj = Application.VBE.ActiveVBProject.References ' removes reference to excel Call ReferencesObj.Remove(ReferencesObj("Excel")) End Sub ' use this method to print the name of the currently selected references in the immediate window. ' then use the name as the key in the remove routine. Public Sub PrintReferences() For Each reference In Application.VBE.ActiveVBProject.References Debug.Print reference.Name, reference.GUID, reference.fullpath Next reference End Sub
Let me know if this helps!
I think this is exactly what I was looking for.
Yes, I have modules that I want to connect to, run code, delete from primary graphic.
I will test this today if I get a chance.
I am happy that I understood correctly Brent!
I also want to let you know that I gave a new title to your question, to make it easier to search in the future and make it more generic.
Hope this is ok for you, you can look at the edit at the end of your question.
Let me know if this works for you!
On purpose I did not went into the "add" way because I thought it was unnecessary and adding references will be challenging if possible.
If you look at the methodology section I wrote earlier, the logic is that you create your code in the template, and you make sure it is generic enough.
Every time you need to create a new displayyou make a copy of the template, and provide the config file, then you proceed with the display creation.
With this logic, modify implies making change to the config file and go over the creation process again by using a new fresh copy of the template.
The proposed logic is one way only.
If you tell me more why you need to add code, maybe we can find a solution.
Sorry adding importing modules.
The module1.bas has been exported I now need to import run and remove
I understand about references as there are many versions of excel and can't ensure same on each machine.
Get Outlook for iOS<https://aka.ms/o0ukef>
To import a new module, you can do this:
Public Sub ImportModule() Application.VBE.ActiveVBProject.VBComponents.Import "c:\temp\modHello.bas" End Sub
If you have a function named "Hello" in the imported module, you will need to wait to have the Module loaded before you can write the line of code that runs the hello method. (e.g. call Hello() ). Because the VBA code wont compile at all otherwise.
I could not find a way to call the code dynamically, this would be required here.