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: 

Save As Operation in VBA

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
shastu
4150 Views, 9 Replies

Save As Operation in VBA

I am wanting to do the equivalent of a Save As function in VBA where it brings up the dialog box for you to input your new file name.  If I just wanted to Save the file it would be oDoc.Save, but what do I use for the Save as command?  Also if I want to put a captured value in the Comments field, what is the code for that?  For example, if I am filling in the "Stock Number" field with the value stockno I use this:  oDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").Item("Stock Number").Value = stockno where stockno is a String Value.

9 REPLIES 9
Message 2 of 10
sajith_subramanian
in reply to: shastu

Hi shastu,

 

For the save as function, you may use the File Dialog API. Sample code from the API help:

 

Public Sub TestFileDialog()
    ' Create a new FileDialog object.
    Dim oFileDlg As FileDialog
    Call ThisApplication.CreateFileDialog(oFileDlg)

    ' Define the filter to select part and assembly files or any file.
    oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"

    ' Define the part and assembly files filter to be the default filter.
    oFileDlg.FilterIndex = 1

    ' Set the title for the dialog.
    oFileDlg.DialogTitle = "Open File Test"

    ' Set the initial directory that will be displayed in the dialog.
    oFileDlg.InitialDirectory = "C:\Temp"

    ' Set the flag so an error will be raised if the user clicks the Cancel button.
    oFileDlg.CancelError = True

    ' Show the open dialog.  The same procedure is also used for the Save dialog.
    ' The commented code can be used for the Save dialog.
    On Error Resume Next
    oFileDlg.ShowOpen
'    oFileDlg.ShowSave

    ' If an error was raised, the user clicked cancel, otherwise display the filename.
    If Err Then
        MsgBox "User cancelled out of dialog"
    ElseIf oFileDlg.FileName <> "" Then
        MsgBox "File " & oFileDlg.FileName & " was selected."
    End If
End Sub
 

 

To access the comments field , you may refer the code below that does something similar.

 

Sub Main()

Dim oPropSet As PropertySet
Set oPropSet = ThisApplication.ActiveDocument.PropertySets("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}")
          
Dim oProp As Property
Set oProp = oPropSet.Item("Comments")
oProp.Value = "Test_Value"

End Sub

 

Let me know if it helps.

 

Regards,

Sajith


Sajith Subramanian
Autodesk Developer Network
Message 3 of 10
shastu
in reply to: sajith_subramanian

O.k. I uncommented the "ofileDlg.Show Save" line and commented out the "ofileDlg.ShowOpen" line and the dialog box comes up but when I type in my new file name and press enter or click on the save button, the dialog box goes away and then the error message box comes up telling me the file was selected. It doesn't save the file and when you use the save as command, the file that it was saved as should then be the current file, but it isn't. Also if I have a part file open, when the error message comes up it is showing iam instead of ipt.

The comments code that you gave works.

Thanks,
Shawn
Message 4 of 10
sajith_subramanian
in reply to: shastu

Hi Shawn,

 

The api ShowSave  does not actually perform a save but only returns the filename the user has specified through the dialog.

Since you are trying to mimic the exact command as the UI, I believe you would find it easier to call the exact command through VBA.

 

Following is a piece of code that calls the SaveAs command through VBA.

 

Sub RunCommand()
    ' Get the CommandManager object.
    Dim oCommandMgr As CommandManager
    Set oCommandMgr = ThisApplication.CommandManager

    ' Get control definition for the line command.
    Dim oControlDef As ControlDefinition
    Set oControlDef = oCommandMgr.ControlDefinitions.Item("AppFileSaveAsCmd")
    ' Execute the command.
    Call oControlDef.Execute
End Sub

 

Additionally, if you want to call the save command instead of the save as command, you could use "AppFileSaveCmd" instead of "AppFileSaveAsCmd" in the above code.

 

Let me know if this approach works better for you.

 

Regards,

Sajith


Sajith Subramanian
Autodesk Developer Network
Message 5 of 10
shastu
in reply to: sajith_subramanian

Thank you so much!!!

Message 6 of 10
shastu
in reply to: sajith_subramanian

Sajith,  One slight problem with this.  When I use it on a part file (.ipt) it works as I would expect.  When I use it on an assembly file (.iam) it does not change the Part Number iProperty to the new file name like it does when I use the "Saveas" command directly in Inventor.  Do you know why that would be?

Message 7 of 10
shastu
in reply to: shastu

I went ahead and just wrote some code to take care of the problem, but am still curious as to why the behavior isn't consistent between parts and assemblies.

Message 8 of 10
shastu
in reply to: sajith_subramanian

Sajith,

 

Any idea why this would be inconsistent?  I am running this multiple times and all of sudden it will stop bringing up the saveas dialog box.  Seems as if I close down Inventor, and then restart it, then it starts working again with the exact same files and code.  Any ideas why that might be?  Even if I step through it using the F8 key, it just goes right on to the next step without bringing open the dialog box.  Any chance this is because the program is throwing an error, and then from that point on it doesn't work?

Message 9 of 10
shastu
in reply to: sajith_subramanian

Sajith,

 

Never mind.  I figured it out.  This was the problem:

 

ThisApplication.SilentOperation = True

 

Now that I am starting to figure some of this out on my own, hopefully I won't be so quick to post messages.  So for those that have been frustrated with how much I have posted lately, Thanks for all your help and hopefully my post will be less.

Message 10 of 10
shastu
in reply to: sajith_subramanian

Is there a way to recognize if the user cancelled out of this dialog box?  If they hit cancel, it still just keeps on going with the same code instead of "Exit Sub"

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report