Hi,
How to make VBA macro from iLogic?
AddReference "AddinNETFramework.AdWebServicesWrapper.dll" Imports Autodesk.WebServices
Have a lovely day.
Do you have an iLogic rule you want one of us to translate so you can use it in a VBA macro. If so, just post it here and we'll review it and see if it's possible, and if so, we can work on it for you. Most basic stuff just needs to be laid out differently, and a few key words included or removed here and there. There are some things that just won't work the same way from one to the other, so sometimes you just have to replace a reference or method with an equivalent one designed for use in VBA.
Wesley Crihfield
(Not an Autodesk Employee)
Thank you very much @dutt.thakar @WCrihfield ,
I have iLogic rule but would like to make VBA macro.
This is the code, very simple just those first two lines and maybe CWebServicesManager(), webServiceMgr.Initialize(), everything else is simple:
AddReference "AddinNETFramework.AdWebServicesWrapper.dll" Imports Autodesk.WebServices
Sub Main Dim firstName As String = "" Dim lastName As String = "" If GetUserName(firstName, lastName) Then MessageBox.Show("Hello, " & firstName & " " & lastName, _ "Personal message") Else MessageBox.Show("Failed to get user info.", "Fail") End If End Sub Public Function GetUserName(ByRef firstName As String, ByRef lastName As String) As Boolean Try Dim webServiceMgr As New CWebServicesManager() If webServiceMgr.Initialize() Then webServiceMgr.GetUserFirstName(firstName) webServiceMgr.GetUserLastName(lastName) If firstName = "" Or lastName = "" Then firstName = "" lastName = "" Return False Else Return True End If Else firstName = "" lastName = "" Return False End If Return True Catch ex As Exception firstName = "" lastName = "" Return False End Try End Function
Have a lovely day.
Well...I got the code all translated, but the reference thing doesn't seem to want to work. I know how to set the reference, but when I try to do it I get a pop-up error message that says "Can't add a reference to the specified file.", for some reason. That reference is located at the following location (by default):
C:\Program Files\Autodesk\Inventor 2021\Bin\AddinNETFramework.AdWebServicesWrapper.dll
And to add it you would normally just (within the VBA Editor dialog) click on the 'Tools' tab, then click on 'References...', which brings up a References dialog. I don't think this item is usually listed, unless it goes by a different, hard to recognize name, so you would click the 'Browse...' button to browse to the specific dll file, and select it. But when I select this file and click the 'Open' button, I get that error. And without that reference being added, it won't recognize the 'CWebServicesManager' object.
I don't know if you still want to go this route or not since I couldn't set the reference needed, but here is the rest of the code (which really didn't need much translation at all):
Sub Main() '<<< I WOULD THIS FROM 'MAIN' TO SOMETHING ELSE >>>
Dim firstName As String
firstName = ""
Dim lastName As String
lastName = ""
If GetUserName(firstName, lastName) Then
Call MsgBox("Hello, " & firstName & " " & lastName, , "Personal message")
Else
Call MsgBox("Failed to get user info.", , "Fail")
End If
End Sub
Public Function GetUserName(ByRef firstName As String, ByRef lastName As String) As Boolean
On Error GoTo ErrorHndlr
Dim webServiceMgr As New CWebServicesManager()
If webServiceMgr.Initialize() Then
webServiceMgr.GetUserFirstName (firstName)
webServiceMgr.GetUserLastName (lastName)
If firstName = "" Or lastName = "" Then
firstName = ""
lastName = ""
GetUserName = False
Else
GetUserName = True
End If
Else
firstName = ""
lastName = ""
GetUserName = False
End If
GetUserName = True
Exit Function
ErrorHndlr:
firstName = ""
lastName = ""
GetUserName = False
End Function
In VBA macros, it's good practice to give your main Sub a unique name (will be the name of your macro), other than "Main", unlike in iLogic. You can try setting that reference on your PC, and see if maybe, for some reason, it will work for you, where it wouldn't work for me. You could also just have the macro run the iLogic rule, instead of putting all the code into the macro, if wanted/needed. Good luck.
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb:or you can Explore My CONTRIBUTIONS
Wesley Crihfield
(Not an Autodesk Employee)
Thank you @WCrihfield for your time and effort,
Is there any other way to get the user name and surname, VBA code?
This probably isn't what your looking for but...
ThisApplication.GeneralOptions.UserName
This is pretty much what you have entered into Inventor's Application Options > General tab > User Name textbox.
If your Inventor users are using their whole names in this field (not just initials or abreviated), then you could use normal String manipulation tools to isolate the first and last names.
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield
(Not an Autodesk Employee)
Thank you @WCrihfield ,
We put initials there, so that's why I need user account name and surname, I have that code from one of the forum users, it's great but iLogic.
The only option, for now, is to run iLogic from the VBA macro, but I do not know how to get value from the iLogic variable to the VBA variable, never did that, and must admit did not search for the solution.
Sending and receiving data between iLogic rules and VBA macros is fairly simple, but I suppose it can seem a bit odd or unexpected. Basically you create a NameValueMap object, then create some name/value pairs within it. One or more of those name/value pairs can be for pushing data to the 'other side', while one or more name/value pairs can just be a name with 'Nothing' as its value, as placeholders for the data you are planning to have returned from the 'other side'. When you use one of the RunRule or RunMacro lines of code that allows you to pass arguments, and you supply this NameValueMap object in place of the arguments, that not only makes that NameValueMap available to the 'other side', but since it was created within the source side, it is still available there too. So the code your using on the 'other side' just needs to find/retrieve that NameValueMap, that contains an expected name/value pair's name, then either retrieve its value, or set its value so the 'other side' can retrieve it.
On the iLogic rule side, you handle (retrieve) NameValueMaps using the RuleArguments interface. Once the rule has retrieved it, it can work with it as you normally would (retrieve values, set values, add or remove pairs). Once the rule is finished, control is returned to the still unfinished VBA macro, which simply accesses the NameValueMap it created earlier, and retrieves one or more values from it. Here is a link to one of my contribution posts where I use this method to send & receive data between two rules.
On the iLogic side you can sometimes also use the SharedVariable interface to share between rules too, but they are defined within the iLogic add-in, so you can't work with them in VBA.
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb:or you can Explore My CONTRIBUTIONS
Wesley Crihfield
(Not an Autodesk Employee)
Thank you @WCrihfield ,
You say it is simple, but just looking at your post, how long it is and how much time you took to answer which I appreciate a lot, it seems not so easy.
I am simple FOR NEXT WHILE DO IF THEN ELSE guy, everything more than that not easy for me.
I Will try to understand what you are talking about, if not I would ask you for more details if possible.
Have a lovely day.
Can't find what you're looking for? Ask the community or share your knowledge.