ThisDoc.Document.SaveAs not working as expected

ThisDoc.Document.SaveAs not working as expected

akAMYTB
Enthusiast Enthusiast
2,161 Views
3 Replies
Message 1 of 4

ThisDoc.Document.SaveAs not working as expected

akAMYTB
Enthusiast
Enthusiast

Hi Everyone

 

I'm working on a bit of code, that will "SaveAs" when Partnumber or Title is updated in iProperties. The purpose is to make a kind of seemless transition from part/assembly files with temporary worktitles, to our companys more consistent filenaming-scheme with PartNumber_Title. Ideally in time, I'd like this function to also update the partfile's dependants and then delete the original file with the worktitle, but for now I'm just just focusing on the first part, and most of the code is working just fine.

 

I call the current filename with the ThisDoc.FileName(False) and compare it to the new filename that is PartNumber, "_" and Title concatenated. If they're not the same, the ThisDoc.Document.SaveAs will run with the new filename. I can get it to work, but only if I also add the path into the beginning of the new filename. So the new file ends up being named: "the folder that its in""PartNumber"_"Title". If I don't add the path, the function doesn't work, I get an error and no new file is created.

 

So is this just the nature of the ThisDoc.Document.SaveAs command, or is there a way to use it without the path in the new filename?

 

Cheers - Anders

0 Likes
Accepted solutions (1)
2,162 Views
3 Replies
Replies (3)
Message 2 of 4

FrodoZhou
Autodesk
Autodesk

Hi Anders,

 

Can you please clarify if you do not want to specify the folder path, where (which folder path) do you want the new files being saved to?

 

If you just want to save the files to the same folder path as the source file, there is easy way you can get the folder path from the source file and apply that to your new file.

 

Frodo

0 Likes
Message 3 of 4

b_sharanraj
Advocate
Advocate

You have to define the path for new file.

 

If you explain us briefly with some example we can do something better.

Regards

B.Sharan Raj

0 Likes
Message 4 of 4

akAMYTB
Enthusiast
Enthusiast
Accepted solution

Hi everyone

 

I got it to work. This is my code. The & "\" highlighted with bold red, is what caused the problem, because it wasn't included. 

 

 

SyntaxEditor Code Snippet

OldFileName = ThisDoc.FileName(True) 'with extension
PartNumber = iProperties.Value("Project", "Part Number") 'Get "PartNumber" from iProperties
PartName = iProperties.Value("Summary", "Title") 'Get "Title" from iProperties
FilePath = ThisDoc.Path & "\" 'Get the path of document

If PartNumber = "" And PartName = "" Then 'Check "PartNumber" and "Title" for content
    Return
Else
    NewFileName = PartNumber & "_" & PartName & ".ipt" 'Generate new file name

    If NewFileName <> OldFileName Then 'Check file name for changes
        ThisDoc.Document.SaveAs(FilePath & NewFileName, False) 'SaveAs with new file name
    End If

End If

 

 Thanks for the help anyway

 

Cheers - Anders