Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Run VBA macro when "Save" is pressed on "IPT" files

13 REPLIES 13
Reply
Message 1 of 14
isocam
4090 Views, 13 Replies

Run VBA macro when "Save" is pressed on "IPT" files

Can anybody help?

 

Is it possible to run a VBA macro every time the "Save" button is pressed on "IPT" files?

 

Kindest Regards

 

IsoCAM

13 REPLIES 13
Message 2 of 14
Anonymous
in reply to: isocam

 

 

search or post this on the customizaton forum:

Autodesk Inventor Customization

Message 3 of 14
Mike.Wohletz
in reply to: isocam

It is very possible, but I will say that it is not going to be as easy to do using VBA as it is if you create an Add-In. For the most of it if you look at the API help under events you will find what you are looking for. 

ApplicationEvents.OnSaveDocument

 

sample 

In a new  Class Module Named AppEvents add the following

 

 

Private WithEvents oAppEvents As ApplicationEvents

Public Sub Connect()
Set oAppEvents = ThisApplication.ApplicationEvents
End Sub

Public Sub disconnect()
Set oAppEvents = Nothing
End Sub

Private Sub oAppEvents_OnSaveDocument(ByVal DocumentObject As Document, ByVal BeforeOrAfter As EventTimingEnum, ByVal Context As NameValueMap, HandlingCode As HandlingCodeEnum)
If BeforeOrAfter = kAfter And DocumentObject.DocumentType = kPartDocumentObject Then ' this will happen after the save

' call your vba code
MsgBox ("you saved the part")

End If
End Sub

 now in Module1 or another module of your choice add the following:

 

 

Option Explicit
Dim appevent As New AppEvents

Public Sub StartEvent()
appevent.Connect
End Sub

Public Sub StopEvent()
appevent.disconnect
End Sub

 

 

 

I hope before it will run you will have to call the StartEvent sub..

 

Message 4 of 14
isocam
in reply to: isocam

Hi,

 

Can I please ask, does this mean that I do not need to create a Addin?

 

Can this be achieved purely by inventors VBA macros. If so could you send me the complete code?

 

Many thanks!!

 

IsoCAM

Message 5 of 14
Anonymous
in reply to: isocam

 If your are running one of the latest versions of Inventor you might want to look at iLogic in your part template file.

Code can be set to run on a "Save" Trigger.

Message 6 of 14
Mike.Wohletz
in reply to: isocam

It is possible, but it is not a recommended way of doing things.  Before saying what is the best way to handle this for you I would need to know what it is that you are trying to achieve, If you want this routine to work on files that you have created in the past then the best method is to create the add-in and while it is possible to call VBA from within the add-in I would recommend moving your VBA code to the add-in as well.  Gruff has your second option that is not a bad one, but it is going to work in the files created after the iLogic has been added to your template and is not going to offer much help to files created prior to the ones created with the new template. While it is possible to have an add-in that will copy iLogic from your current template to previously created parts I would have to suggest that you go for the add-in from the start and then you are done.  I dont know what language you are programming with, but I have some samples of this in VB.NET. If this would be helpful please let me know and I will post it for you.

Thanks

Mike W.

Message 7 of 14
isocam
in reply to: Mike.Wohletz

Hi Mike,

 

Many thanks for your help!!!

 

All that I am trying to do is to run a macro in the "Default.ivb" file that runs every time the "SAVE" button is pressed.

 

If you can send me any vb.net projects that you have, that can help, I would be extremely grateful.

 

Kindest Regards

 

IsoCAM

Message 8 of 14
Mike.Wohletz
in reply to: isocam

I have that for you, please post your module name and the name of the sub that you want to run and I will add that info in and then post a working vb.net 2008 project for you to try out..

 

Message 9 of 14
isocam
in reply to: isocam

Hi Mike,

 

The module name is Module01 and the SubRoutine name is "Main"

 

eg

 

Attribute VB_Name = "Module01"

 

Public Sub Main()
    Msgbox "Test"

    
    End
End Sub

 

Many thanks for your help on this subject!!!!

 

Kindest Regards

 

IsoCAM

Message 10 of 14
Mike.Wohletz
in reply to: isocam

Please find the Project attached.  You can change the module name and the sub name to change in the bottom of the StandardAddInServer.vb file. I have compiled this and if you wish to test it with the sub names that you have given me you can do so by registering the add-in with the appropriate .bat file located in the bin folder.

 

Message 11 of 14
isocam
in reply to: isocam

Mike,

 

I downloaded and installed the dll. Everything is superb!!!!

 

MANY THANKS FOR YOUR HELP, VERY MUCH APPRECIATED!!!

 

Kindest Regards

 

IsoCAM

Message 12 of 14
isocam
in reply to: isocam

The macro "SaveRunMacro.dll" has worked fine on Autodesk Inventor 2010.

 

I am now running Autodesk Inventor 2012 (Windows 7), and it does not seem to work!

 

Can anybody tell me what is wrong?

 

I have installed/registered the dll by using the bat file supplied.

 

Many thanks in advance!!!!

Message 13 of 14
Mike.Wohletz
in reply to: isocam

I have tested this with Inv 2012 on Windows & 64 bit and have no problems with it other than it is very slow. I have rebuilt this with a couple changes in reference files for you to try out. You should be able to just replace the SaveRunMacro.dll that you have with the new one and see what happens. I don't know that all is in the sub Main vba file, but if it is possible it would work much better to include that into the adding and skip the entire VBA part all together.

 

Message 14 of 14
isocam
in reply to: isocam

Hi All,

 

I am running the macro, attached below!

 

I seem to have the problem that Inventor wants to run the "Document Project" firstly. I find myself having to go into the VBA editor and run the macro first. Then everything seems OK.

 

Is there a way of skipping the "Document Project" so that the "SaveRunMacro" executes at every "Save" button press?

 

Can anybody help?

 

IsoCAM

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report