- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Issues with my first macro
I am just getting into using Inventor's API, and I would like some help getting in the right direction.
I found a basic Autosave macro online:
Public Sub TimerSetting()
i = InputBox("Input Timer", "Timer Form")
i = i * 60
If i > 600 Then
MsgBox ("30min MAXIMUM, TIMER SET TO 30min")
i = 600
ElseIf i < 60 Then
MsgBox ("1min MINIMUM, TIMER SET TO 1min")
i = 60
End If
Dim SetTimer, Start, Finish, TotalTime
SetTimer = i ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + SetTimer
DoEvents ' Yield to other processes.
Loop
Finish = Timer ' Set end time.
TotalTime = Finish - Start ' Calculate total time.
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
oDoc.Save2 (True)
MsgBox "DOCUMENT HAS BEEN SAVED"
End SubIt would run as long as you didn't cancel anything, but I wanted to set some more boundaries:
1) I want to look at the active document, identify its type, and tell the user that it should only be used in drawing documents
2) I want to rune the timer, basically the way it is written above, and save the previously identified document [ I want to be able to switch between multiple part/assembly documents while timer is running and save the drawing upon returning after the timer is done]
3) I want to ask the user if they would like to restart the timer, and do so with the same time limit
Here is what I've come up with so far:
Public Sub AUTOSAVE()
' Connect to a running instance of Inventor.
Dim invApp As Inventor.Application
invApp = System.Runtime.InteropServices.Marshal.GetActiveObject _
("Inventor.Application")
' Get the Documents collection.
Dim oDocs As Inventor.Documents
oDocs = invApp.Documents
' Get the Active part document.
Dim ThisDoc As Inventor.Documents
ThisDoc = invApp.ActiveDocument
If ThisDoc = Inventor.DrawingDocuments Then 'Check if user is in a drawing document
GoTo 100
Else
o = MsgBox("I would recommend only using the AUTOSAVE macro in drawing documents. Would you like to ignore this warning?", "Caution", MessageBoxButtons.YesNo)
If o = vbYes Then
MsgBox "Good Luck..."
GoTo 100
Else
MsgBox "Good Choice!"
GoTo 102
End If
End If
100 'Requesting user input for timer duration
i = InputBox("This timer has a limited range: 1-30(min).", "DRAFTING AUTOSAVE", i = 600) '10 minute default
i = i * 60
If i > 1800 Then
MsgBox ("This timer has a limited range: 1-30(min), timer was set to 30min.")
i = 1800
ElseIf i < 60 Then
MsgBox ("This timer has a limited range: 1-30(min), timer was set to 1min.")
i = 60
ElseIf i = vbCancel Then
MsgBox "Timer was cancelled."
GoTo 102
End If
101 'Executing timer
Dim SetTimer, Start, Finish, TotalTime
SetTimer = i ' Settting duration.
Start = Timer ' Setting start time.
Do While Timer < Start + SetTimer
DoEvents ' Yield to other processes.
Loop
Finish = Timer ' Settting end time.
TotalTime = Finish - Start ' Calculating total time.
ThisDoc.Save2 (True) 'Saving file at end of timer
'Asking user to run timer again
r = MsgBox("The document has been saved sucessfully, would you like to restart the timer?", "DRAFTING AUTOSAVE", MessageBoxButtons.YesNo)
If r = vbYes Then
MsgBox "Timer will restart now."
GoTo 101
Else
MsgBox "Timer is ended. Happy Modeling!"
GoTo 102
End If
102
End SubMy issue is that the debugger won't get past the 4th line, where I set invApp = to the application. I have tried using the ThisApplication designation but it doesn't like that either.
Can someone point me in the right direction?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
invApp = thisapplication
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've tried that, it does not fix the issue. I get this:
After i click OK, ThisApplication is highlighted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
For VBA you need to "Set" every variable!!!
like
Dim AA as Application
set AA = Thisapplication
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
To my knowledge ThisApplication is a snippet made for use in iLogic, the VBA editor can't use the iLogic snippets, it has to use straight VBA code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
An short snippet and example of how to do it in VBA:
Public Sub AUTOSAVE()
' Connect to a running instance of Inventor.
Dim invApp As Inventor.Application
Set invApp = Thisapplication 'System.Runtime.InteropServices.Marshal.GetActiveObject _
("Inventor.Application")
' Get the Documents collection.
Dim oDocs As Inventor.Documents
Set oDocs = invApp.Documents
' Get the Active part document.
Dim ThisDoc As Inventor.Documents
Set ThisDoc = invApp.ActiveDocument
Set APE as Integer
APE =1
Set NOTE as Boolean
NOTE = True
Set BANANAN as string
BANANA = "This Banana"For I-logic all the sets are not neccessary...
Regards,
Autodesk Software: Inventor Professional 2018 | Vault Professional 2018 | Autocad Mechanical 2018
Programming Skills: Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Dimension Component! | Partlist Export! | Derive I-properties! | Vault Prompts Via API! | Vault Handbook/Manual!
Drawing Toggle Sheets! | Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
That helps a lot, I didn't know about "Set". Is there something I should Set my InputBox variable as? It worked in the original snippet of code without Set, but I want to Develop proper coding grammar as I learn more.
Now I need to find a way to check the type of document that ThisDoc becomes after setting it to ActiveDocument.
I'm not sure how to DocumentDescriptorsEnumerator correctly, it doesn't like my Set line for ThisDocType:
' Get the type of active document Dim ThisDocType As ObjectTypeEnum Set ThisDocType = DocumentDescriptorsEnumerator.Type(ThisDoc)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Okay I got the check right,
' Get the Active part document.
Dim ThisDoc As Inventor.Document
Set ThisDoc = invApp.ActiveDocument
If ThisDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then 'Check if user is in a drawing document
GoTo 100
Else
o = MsgBox("I would recommend only using the AUTOSAVE macro in drawing documents. Would you like to ignore this warning?", "Caution", MessageBoxButtons.YesNo)
If o = vbYes Then
MsgBox "Good Luck..."
GoTo 100
Else
MsgBox "Good Choice!"
GoTo 102
End If
End Ifbut I think I need to "Dim" and "Set" my MsgBox.
Not sure what to Dim o as though
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Okay, I got this thing working without breaking. I want to add a wait loop in case the timer runs out but I am working in a different document at the time, but ith keeps skipping through my loop:
102 ' Waiting till part can be saved, then saving
' Get the Active part document.
Dim CurrDoc As Inventor.Document
Set CurrDoc = invApp.ActiveDocument
Do While CurrDoc <> ThisDoc
DoEvents ' Yield to other processes.
Set CurrDoc = invApp.ActiveDocument
Loop
ThisDoc.Save2 (True) 'Saving file at end of timer
'End 102 Waiting till part can be saved, then savingit is not saving the file because i'm not active in ThisDoc when the timer runs out, and carries on to my restart MsgBox.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
' Get the Active part document.
Dim CurrDoc As Inventor.Document
Set CurrDoc = invApp.ActiveDocument
Do While CurrDoc <> ThisDoc
DoEvents ' Yield to other processes.
' This is already set before!!! Set CurrDoc = invApp.ActiveDocument
Loop
ThisDoc.Save2 (True) 'Saving file at end of timer
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Yeah the extra Set CurrDoc was only added because the macro wasn't staying in the loop while i was working in another document, and I thought it was because CurrDoc was set outside of the loop and it wouldn't keep checking what document was active. I still don't have the macro waiting for me to switch back to ThisDoc, but it will save ThisDoc while I'm in another document so that works for now.
@bradeneuropeArthurI would like to thank you for telling me about using "Set". That was my biggest hang up at the beginning. If you can think of a better way to postpone the save until I'm back in ThisDoc, I would love to hear it. I'm going to use it as is for now though.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
The Set statement isn't used in all assignments, but only when assigning an object to a variable.
But I want to take a step back and let you know that you're just heading towards a lot of disappointment with the program you're trying to write. A VBA macro is not the right architecture to be building automatic save functionality on. When you run a VBA macro, it takes over the Inventor user-interfaces. Like all user-interfaces that I'm aware of, it is single threaded so there's only one thing going on at a time. In your code you're trying to work around this by using the DoEvents statement. What's happening here is that your macro has control over the UI and in the DoEvents statement is temporarily turning control back to Inventor so it can handle any pending events, and then control goes back to your macro. This is very inefficient and I suspect there will be other problems. I would suggest looking for something else as your introduction to learning the API.
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
You are right, I just started testing and have already run into glitchy issues with the autosave macro... Have you ever tried to implement an Autosave feature into Inventor?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
Yes I did it in the past.
but build in macros don't have my preference.
Why are you asking, do you need an example?
Actually this is not possible anymore without using an Add in:
Regards,
Autodesk Software: Inventor Professional 2018 | Vault Professional 2018 | Autocad Mechanical 2018
Programming Skills: Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Dimension Component! | Partlist Export! | Derive I-properties! | Vault Prompts Via API! | Vault Handbook/Manual!
Drawing Toggle Sheets! | Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Besides the architecture issues that I mentioned earlier, creating an auto-save routing for Inventor is a bit tricky because of the save limitations in Inventor and that you don't want it to cause problems for the user. One issue is that you can't save while a sketch is active. I think there may also be some other cases where a save is blocked. Another issue is with assemblies. Do you only save whatever is currently active or do you look through all of the referenced parts and save any that have been modified? And another issue is what is the timing of the save? You don't just want it to be based on a time clock because when you start the save it will terminate whatever is currently going on. So if the user is close to the end of executing a complicated command and they lose all of that because the auto-save terminates it, I don't think they'll be happy. As you can see, it's not a simple problem.
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I never wanted to use the timer in parts/assemblies, just drawing files. I have a bad habit of doing a lot in drawing files without saving, sometimes several hours in big drawings, and I've lost large amounts of time to a crash because of this. I have been getting better about saving often in drawings and crashes aren't very frequent, especially as I learn how to use Inventor better.
I know having an autosave feature would have saved my colleagues and myself a whole lot of re-do time, so I thought it would be a decent/useful project to get into Inventor's API. I'm not going to use my macro because of the issues I've already seen, but I did learn a lot about API grammar so it wasn't a total waste of time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
To my optic we should forget about the auto-macros in any environment, because of the issues related to them.
The only good way is start programming add ins based on the Save-events or other Events in inventor.
Only this way you are able to eliminate all the above or written problems....
If help is needed regarding this, I am willing to support.
Regards,
Autodesk Software: Inventor Professional 2018 | Vault Professional 2018 | Autocad Mechanical 2018
Programming Skills: Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Dimension Component! | Partlist Export! | Derive I-properties! | Vault Prompts Via API! | Vault Handbook/Manual!
Drawing Toggle Sheets! | Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !