Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
J-Camper
931 Views, 16 Replies

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