How can I call a macro with arguments?

How can I call a macro with arguments?

Anonymous
Not applicable
1,869 Views
6 Replies
Message 1 of 7

How can I call a macro with arguments?

Anonymous
Not applicable

Hi

 

How can I pass arguments from iLogic to a vb macro?

 

I have tried the approach which we normally use when passing arguments between iLogic rules. But I am missing something in the process of passing the arguments to the VBmacro. Below is a simplified version of the problem. I want to control which sheet in the excel workbook opens...

 

Here is what I have:

 

iLogic code

 

Dim Value_1 As Integer
Value_1 = 1

Dim map As Inventor.NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
map.Add("Arg", Value_1)

InventorVb.RunMacro("DocumentProject", "Access_excel", "openexcel", map)

 

Vb macro

Sub openexcel()
Dim oApp As Inventor.Application
Dim oOleRef As ReferencedOLEFileDescriptor
Dim oWB As Excel.Workbook
Dim oSheet As Excel.WorkSheet
Dim oChart As Excel.Chart

Set oApp = ThisApplication
Set oOleRef = oApp.ActiveDocument.ReferencedOLEFileDescriptors.Item(1)

Call oOleRef.Activate(kEditOpenOLEVerb, oWB)


Value_1 = RuleArguments("Arg")


If RuleArguments.Exists("Arg") Then

    Set oSheet = oWB.Sheets("Sheet1")
    oSheet.Activate
    MsgBox (oSheet.Name)
   
    Else
   
    Set oChart = oWB.Charts("Chart1")
    oChart.Activate
    MsgBox (oChart.Name)
End If


End Sub

 

 

A workaround is to make two macros in VB and call them individually. But passing arguments to VB could also come in handy for other codes that we are working with.

 

0 Likes
Accepted solutions (1)
1,870 Views
6 Replies
Replies (6)
Message 2 of 7

MechMachineMan
Advisor
Advisor

I think by definition macros are intended to not be passed arguements, and to just be like a subroutine.

 

Try using external rules or internal rules instead, and having the rule ran by a macro so that you can make a button for it easily (if that is why you made it a macro in the first place).


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 7

Anonymous
Not applicable

Hi

 

It should be possible to pass arguments to a vb macro.

 

iLogic overview:

http://help.autodesk.com/view/INVNTOR/2014/ENU/?guid=GUID-BEBDED71-ED75-4329-ADBA-2E5DDCCF8DE8

 

Advanced techniques to write iLogic rules:

http://help.autodesk.com/view/INVNTOR/2014/ENU/?guid=GUID-8DF6F761-1634-4D26-B13A-58AF275FD6F8

 

 

0 Likes
Message 4 of 7

adam.nagy
Autodesk Support
Autodesk Support

Hi,

 

I'm not sure which part of the linked online document you were referring to, but I did not find anything there that is about calling a VBA macro with parameters - only about calling Rules with parameters.

 

Also notice that there is no "RunMacro (with map)" in the function browser tree of iLogic:

RunMacro.png

 

What Justin said sounds correct to me.

Passing parameters to a VBA script has never been a built-in feature as they are supposed to run on their own.

 

Nevertheless, you can always store values somewhere in memory that is accessible to both VBA and iLogic, e.g. the clipboard.

 

You could even highjack the PostPrivateEvent/PeekPrivateEvent functions to pass a string to a macro:

 

iLogic code

Dim cmdMgr = ThisApplication.CommandManager
  
' Do this part from iLogic before calling the VBA macro
cmdMgr.PostPrivateEvent(PrivateEventTypeEnum.kStringEvent, "Hello")

InventorVb.RunMacro("ApplicationProject", "Module1", "PostPeekEvent")

 

VBA Macro inside ApplicationProject >> Module1

Sub PostPeekEvent()
  Dim cmdMgr As CommandManager
  Set cmdMgr = ThisApplication.CommandManager

  Dim text As Variant
  Call cmdMgr.PeekPrivateEvent(kStringEvent, text)
  
  Call MsgBox(text)
End Sub

Cheers, 



Adam Nagy
Autodesk Platform Services
0 Likes
Message 5 of 7

Anonymous
Not applicable

Hi

 

Thanks for the answers.

 

Sorry I must have misread the help file on the chapter regarding "Rule arguments"

 

Adam

You show a nice workaround but I think that this is over my head 😉

I will stick to iLogic...

 

 

There was an other link were this was mentioned:

 

https://forums.autodesk.com/t5/inventor-general-discussion/ilogic/td-p/2611136

 

Regards

Kari

 

0 Likes
Message 6 of 7

adam.nagy
Autodesk Support
Autodesk Support

Wow, you were right after all, Kari! 🙂

 

That makes things simpler:

http://adndevblog.typepad.com/manufacturing/2015/11/pass-parameters-to-a-vba-macro-from-an-ilogic-ru... 



Adam Nagy
Autodesk Platform Services
0 Likes
Message 7 of 7

Anonymous
Not applicable
Accepted solution

Hi

 

Yes it was even simpler than passing mapped values between iLogic rules...

 

The initial "simplified" rule above was solved as follows...

 

iLogic

 

MultiValue.SetList("d_macro_arg", "Chart1", "Sheet1")

' do the selection... Code

InventorVb.RunMacro("DocumentProject", "Access_excel", "openexcel", d_macro_arg, "Thanks Adam")

 

 

Vb macro

Sub openexcel(argument1 As String, argument2 As String)
Dim oApp As Inventor.Application
Dim oOleRef As ReferencedOLEFileDescriptor
Dim oWB As Excel.Workbook
Dim oSheet As Excel.WorkSheet
Dim oChart As Excel.Chart

 

Set oApp = ThisApplication
Set oOleRef = oApp.ActiveDocument.ReferencedOLEFileDescriptors.Item(1)

Call oOleRef.Activate(kEditOpenOLEVerb, oWB)

 

 

If argument1 = "Sheet1" Then
    Set oSheet = oWB.Sheets("Sheet1")
    oSheet.Activate
    'MsgBox (oSheet.Name)
    Else
    Set oChart = oWB.Charts("Chart1")
    oChart.Activate
    'MsgBox (oChart.Name)
End If

 

MsgBox (argument2)


End Sub

 

 

Regards

Kari

0 Likes