Save a document using Inventor VBA Macro

Save a document using Inventor VBA Macro

isocam
Collaborator Collaborator
7,373 Views
10 Replies
Message 1 of 11

Save a document using Inventor VBA Macro

isocam
Collaborator
Collaborator

Can anybody help?

 

I am trying to automatically save an inventor document (ipt, idw or iam) using a VBA macro.

 

here is my code so far....

 

Dim oDoc As Document

Set oDoc = ThisApplication.ActiveDocument

If oDoc Is Nothing Then Exit Sub

oDoc.Save

 

But Inventor keeps crashing every time I run the macro.

 

What is wrong?

 

Many thanks in advance!!!!

 

IsoCAM

 

 

 

 

7,374 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
I think Save needs argument of file name/path.
Because oDoc.Save brings up the save dialog which prompts for filename

I tried you code Inventor does not crash, but prompts the Save Dialog for filename for new file.

For a file which is already saved and if i run this macro, it runs successfully and the file is saved.
0 Likes
Message 3 of 11

Anonymous
Not applicable

In the past I have used...

 

Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument

oDoc.Save 'Save.

 

 

0 Likes
Message 4 of 11

rjay75
Collaborator
Collaborator

Testing your code in Invent 2013 didn't crash it.

 

However there something to note, if the document is a new document and you call the Save method on it as you are doing it should display the dialog prompt for a filename. But if somewhere before this point you have SilentOperation set to true, [ThisApplication.SilentOperation = True], then it will display an error message.

 

 

0 Likes
Message 5 of 11

isocam
Collaborator
Collaborator

Can somebody test the following code?

 

Sub savepart()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument

oDoc.Save 'Save.
End Sub

 

I think it does not work!!!!

 

Please help???

 

Regards

 

Darren

0 Likes
Message 6 of 11

Anonymous
Not applicable

Darren,

 

Your code works only for the document which is previously saved.

This does not work for new document.

 

Check the earlier comments... 

0 Likes
Message 7 of 11

isocam
Collaborator
Collaborator

Hi,

 

Did you actually try the code?

 

The part is existing. Running the code does not update the file.

 

Please try the code and see what I mean.

 

Regards

 

Darren

0 Likes
Message 8 of 11

Anonymous
Not applicable

Yes i did try your code...

I get Save Dialog for new document.

If the document is already saved then it saves sucessfully

0 Likes
Message 9 of 11

ekinsb
Alumni
Alumni

I tried the code too and also get the Save As dialog.  That is by design and is the same behavior you see interactively in Inventor.  The first time you save a document the Save As dialog appears so you can specify the location and file name to create.  Saving after that doesn't display a dialog but just performs the save to the existing document.

 

To save a new document using the API and bypassing the dialog you need to use the Document.SaveAs method. The first argument is the file name and the second argument should be False, indicating you are doing a Save As rather than a Save Copy As.

 

You can check to see if a document has been saved by checking the Document.FullFilename.  If this returns an empty string then the document has never been saved.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 10 of 11

DonStauffer99
Advocate
Advocate

Document.Save still crashes Inventor 2018.

0 Likes
Message 11 of 11

ali.shahmirzadi
Contributor
Contributor

you need to use something like this

 

Call oDrawDoc.SaveAs("c:\temp\test.idw", False) 

 

it works fine then, hope its wat you are looking for

0 Likes