Runtime Error with VBA Code

Runtime Error with VBA Code

Anonymous
Not applicable
1,980 Views
12 Replies
Message 1 of 13

Runtime Error with VBA Code

Anonymous
Not applicable

I'm having a bit of a problem with the SaveAs call in this piece of code. Everything else is working fine and the needed paths are getting passed to the sub.  The SaveAs bit is returning a runtime error. Any advice is greatly appreciated as I wade through this very small part of a larger problem. Thanks!

 

Sub SaveNew(fPath)
    
    'declare the new assembly
    Dim newAsm As AssemblyDocument
    'set the assembly to the target assembly
    Set newAsm = ThisApplication.Documents.Open(fPath, False)
    'save as on the target assembly - passed from GetPath()
    newAsm.SaveAs newPath, False
    'close the target assembly
    newAsm.Close (False)
0 Likes
Accepted solutions (1)
1,981 Views
12 Replies
Replies (12)
Message 2 of 13

bradeneuropeArthur
Mentor
Mentor
Newpath is not declared

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 13

Anonymous
Not applicable

Thanks, but what isn't showing is the rest of my code. newPath is global.

Public newPath As String
Public filePath As String

Public Sub PlaceCompartment()
    
    'get the object from the active document
    
    Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
    
    'on run, display the different options with a GUI and buttons - also sets the filepath to the desired assembly file
    
    AsmForm.Show
    
    'get new path
    
    Call GetPath
        
    'perform save as of needed assembly and parts
    
    Call SaveNew(filePath)
    
    'replace new assembly components with newly created components
    
    'ComponentReplace(
    
    
End Sub


Public Function GetPath() As String
    
    'set up the dialog
    Dim fileDlg As FileDialog
    
    'create file dialog object
    Call ThisApplication.CreateFileDialog(fileDlg)
    
    'define filter
    fileDlg.Filter = "Inventor Files (*.iam)|*.ipt | Assembly File (*.iam)|*.iam"
    
    'define the part and assembly files filter to be the default filter
    fileDlg.FilterIndex = 1
    
    'file dialog title
    fileDlg.DialogTitle = "Save File As"
    
    'throw error when user hits cancel
    fileDlg.CancelError = True
    
    'show file dialog
    On Error Resume Next
    fileDlg.ShowSave
    
    newPath = fileDlg.FileName
    
       
       
End Function

Sub SaveNew(fPath)
    
    'declare the new assembly
    Dim newAsm As AssemblyDocument
    'set the assembly to the target assembly
    Set newAsm = ThisApplication.Documents.Open(fPath, False)
    'save as on the target assembly - passed from GetPath()
    newAsm.SaveAs newPath, False
    'close the target assembly
    newAsm.Close (False)
0 Likes
Message 4 of 13

bradeneuropeArthur
Mentor
Mentor
Your new name already exist....

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 5 of 13

Anonymous
Not applicable

@bradeneuropeArthur

 

I'm not really following. Can you be more descriptive than "your new name already exists"? Not at all trying to be jerk, I just don't understand what you're trying to tell me. The variable newPath already exists? The newly created AssemblyDocument exists? If the variable, I'm not exactly sure what the problem is - it is declared globally and updated along the way to the SaveNew sub (the file path it contains within SaveNew() is correct from GetPath()). Even if I change it to a literal path, I still get the runtime error.

0 Likes
Message 6 of 13

bradeneuropeArthur
Mentor
Mentor
You select a file in a filedialog. So I think assume the file exist

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 7 of 13

Anonymous
Not applicable
I don't select anything in the file dialog. I only browse to where I want abd create the path that is used for the save as. File fialog doesnt create anything new, it just returns a string.
0 Likes
Message 8 of 13

bradeneuropeArthur
Mentor
Mentor
Hi.

I assume or am I not correct with:

newPath = fileDlg.FileName

The file is already there, so an existing file?

Or am I wrong.

Understand what I mean

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 9 of 13

Anonymous
Not applicable
There is no file there. It is simply a text string holding what I want the file to be named. It's a save dialog box, not an open and it doesn't actually save anything, hence no file.
0 Likes
Message 10 of 13

bradeneuropeArthur
Mentor
Mentor

What happens if you fill in the newpath as string like c:\test.iam?
Does it work than?

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 11 of 13

bradeneuropeArthur
Mentor
Mentor
Accepted solution

Hi Found I think:

 

The filedialog always saves as .ipt

 

look in the messagebox

 

Public Function GetPath() As String
    
    'set up the dialog
    Dim fileDlg As FileDialog
    
    'create file dialog object
    Call ThisApplication.CreateFileDialog(fileDlg)
    
    'define filter
    fileDlg.Filter = "Inventor Files (*.ipt)|*.ipt | Assembly File (*.iam)|*.iam"

'OPTION SULUTION:
'fileDlg.Filter = "Inventor Files (*.iam)|*.iam ' THIS WILL FUNCTION ONLY FOR ASSEMBLIES 'OPTION SULUTION:
'define the part and assembly files filter to be the default filter fileDlg.FilterIndex = 1 'file dialog title fileDlg.DialogTitle = "Save File As" 'throw error when user hits cancel fileDlg.CancelError = True 'show file dialog On Error Resume Next fileDlg.ShowSave newPath = fileDlg.FileName MsgBox newPath ' RESULT ALWAYS *.IPT End Function

 

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 12 of 13

Anonymous
Not applicable

I was literally just typing up the same thing! I realized it when I sat down at my desk this morning and it works perfectly fine now. Thanks for the help!

0 Likes
Message 13 of 13

bradeneuropeArthur
Mentor
Mentor

Hi,

 

My pleasure to help.

Your thanks are appreciated.

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes