Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Issues with my first macro

J-Camper
Advisor

Issues with my first macro

J-Camper
Advisor
Advisor

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 Sub

It 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 Sub

My 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?

0 Likes
Reply
926 Views
16 Replies
Replies (16)

bradeneuropeArthur
Mentor
Mentor
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 !

0 Likes

J-Camper
Advisor
Advisor

I've tried that, it does not fix the issue. I get this:

 

error report.JPG

 

After i click OK, ThisApplication is highlighted

0 Likes

bradeneuropeArthur
Mentor
Mentor

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 !

philip1009
Advisor
Advisor

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.

0 Likes

bradeneuropeArthur
Mentor
Mentor

@J-Camper

 

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 !

0 Likes

J-Camper
Advisor
Advisor

@bradeneuropeArthur

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)
0 Likes

J-Camper
Advisor
Advisor

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 If

but I think I need to "Dim" and "Set" my MsgBox.

 

Not sure what to Dim o as though

0 Likes

J-Camper
Advisor
Advisor

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 saving

it is not saving the file because i'm not active in ThisDoc when the timer runs out, and carries on to my restart MsgBox. 

0 Likes

bradeneuropeArthur
Mentor
Mentor
' 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 !

0 Likes

J-Camper
Advisor
Advisor

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.

0 Likes

BrianEkins
Mentor
Mentor

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
0 Likes

J-Camper
Advisor
Advisor

@BrianEkins

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?

0 Likes

bradeneuropeArthur
Mentor
Mentor

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:

https://forums.autodesk.com/t5/inventor-customization/inv-2014-removes-support-for-vba-auto-run-macr...

 

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 !

0 Likes

BrianEkins
Mentor
Mentor

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

J-Camper
Advisor
Advisor

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.

 

 

0 Likes

bradeneuropeArthur
Mentor
Mentor

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 !

0 Likes