Check if file is saved, special case

Check if file is saved, special case

pball
Mentor Mentor
1,620 Views
3 Replies
Message 1 of 4

Check if file is saved, special case

pball
Mentor
Mentor

I just stumbled across a case where the normal procedure I use to see if a file is saved or unsaved does not work. In many code examples I've seen it checks the document.fullfilename and if that is something the file has been saved. I've seen an Autodesk employee recomend using that check and it works most of the time.

 

The case I just found is when working inside of an assembly and either demoting a part to a new assembly or creating a new part using the create component command. Both of these commands have you choose a filepath for the new assembly/part and if you check the document.fullfilename for that newly created assembly/part it exists. The issue is until you do a save that assembly/part is not saved to the disk.

 

So for instance if you check the document.fullfilename it will exist (normally this means the assembly/part has been saved) and then a script tries the following. The following check will error because while dpath is the full file name for that component, the component has not been saved to disk yet.

 

dpath = oDoc.fullfilename

if (ThisApplication.Documents.Open(dpath, False).DocumentType <> kAssemblyDocumentObject) then

 

I'd like to know if there is another simple way to check if a file is actually saved or would that require making a function to check if the file exists on disk.

 

Secondly I got around the error in the code above by chaning it to use oDoc.DocumentType instead of trying to open the file (not sure why I did it that way originally), but I have one other problem. I use the code below to open a choosen part by itself and it doesn't work for the reason stated above. Is there a way to use ThisApplication.ActiveEditDocument to open an assembly/part in a new window? Thanks

 

Set oPartDoc = ThisApplication.Documents.Open(dpath, True)
oPartDoc.Activate

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Accepted solutions (1)
1,621 Views
3 Replies
Replies (3)
Message 2 of 4

nmunro
Collaborator
Collaborator
Accepted solution

You might try the FileSaveCounter property of the document. From the help file "Gets the number that indicates the number of times the file has been saved". I have used this successfully to check if a document has been saved.

 

Neil

        


https://c3mcad.com

Message 3 of 4

pball
Mentor
Mentor
Thanks, that works great. I'm going to update all my scripts to use that since it seems bullet proof.

It'd be interesting to see if there is an answer to my secondary question but it's not really important now.
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 4 of 4

CattabianiI
Collaborator
Collaborator

For the ones who came here looking for a solution in Inventor 2022 (and followings), check this post:
https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/how-to-check-if-a-file-has-been-saved-i...

0 Likes