Hi, I want to share a solution for my question about programming the VBA editor of Inventor.
In summary, I wanted a VBA code to run only one time and able to create programmatically several VBA subroutines inside a module (TempModule for example) of the opened/active Inventor object (ipt, iam, idw, ...). In this way I can execute an external rule that executes a specific VBA subroutine inside TempModule and I can stop it with the common Ctrl+Break (only valid for VBA and not for iLogic).
The solution I found is inspired by an answer provided by @bradeneuropeArthur that can be found here. Thanks @bradeneuropeArthur!
Procedure tested on Inventor 2018 Pro (if you are interested ;-)):
1) Copy inside Default.ivb of Inventor the following code:
Sub AutoMacroAdd()
'' This to enable SendKeys Alt+F11 in order to open VBA editor (without it we obtain an error at "Set VBProj = ..."):
Dim dTILL As Double
SendKeys ("%{F11}"), True
dTILL = Now + TimeSerial(0, 0, 1)
Do While dTILL > Now: DoEvents: Loop
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Set VBProj = ThisApplication.ActiveDocument.VBAProject.VBProject
On Error Resume Next
Dim Element As Object
For Each Element In VBProj.VBComponents
VBProj.VBComponents.Remove Element
Next
Set VBComp = VBProj.VBComponents.Add(vbext_ct_StdModule)
VBComp.Name = "TempModule"
Set CodeMod = VBComp.CodeModule
Dim strFilename As String: strFilename = "C:\VBA - Test_for_iLogic_Browser.vb"
Dim strFileContent As String
Dim iFile As Integer: iFile = FreeFile
Open strFilename For Input As #iFile
strFileContent = Input(LOF(iFile), iFile)
Close #iFile
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, strFileContent
End With
'' This to close VBA editor:
SendKeys ("%{F4}")
End Sub
2) Enable option "Microsoft Visual Basic for Applications Extensibility 5.3" in VBA editor (Tools, References..., ), save Default.ivb by closing Inventor and reopening it
3) Create the VBA macro "VBA - Test_for_iLogic_Browser.vb" in C: for the test, with the following code:
Sub Test_for_iLogic_Browser()
Dim i As Integer
For i = 1 To 4
MsgBox ("Test" + Str(i))
Next
Call finish()
End Sub
Sub finish()
MsgBox ("Finish.")
End Sub
4) Create two external iLogic rules (in iLogic language) in the path you want and with the following content:
for iLogic rule named "Initialize VBA Modules.vb":
InventorVb.RunMacro("ApplicationProject", "Module1", "AutoMacroAdd")
for iLogic rule named "Run VBA - Test_for_iLogic_Browser.vb":
InventorVb.RunMacro("DocumentProject", "TempModule", "Test_for_iLogic_Browser")
5) Now create a new Inventor file (ipt, iam, etc..) and execute the external rule "Initialize VBA Modules" in your iLogic browser: it should create programmatically in the VBA editor the code "VBA - Test_for_iLogic_Browser.vb" in a module named "TempModule".
6) Now execute the external rule "Run VBA - Test_for_iLogic_Browser" in your iLogic browser: it should execute the VBA subroutine "Test_for_iLogic_Browser" that you can interrupt it with Ctrl+Break (very useful when developing a new code).
The only required iLogic codes are the ones you need to execute VBA macros.
The main external rules executed for Inventor purposes (renaming iProperties of all files, hiding of all parts inside a close path, etc...) must be in VBA language, because only with it you can stop execution.
Reasons: I find iLogic browser more comfortable than the VBA code selector (when opened it freezes entire Inventor application, like the iLogic editor) but I prefer VBA environment and editor than iLogic one because user can modify VBA code on the fly without closing the editor and you can stop execution of all codes (impossible with iLogic).
I will try to improve the code by changing the code line "Dim strFilename As String: strFilename = "C:\VBA - Test_for_iLogic_Browser.vb"" in order to have a Default.ivb file independet from specific VBA codes.
Hope the procedure I share helps someone like me about programming.
Regards