I am trying to figure out this code I found on the community forum. And the only error I get with it is on a few lines that refer to "Path." I don't know what "Path" is supposed to do. Is it a VBA function or just a name?
Here is the code I found that is supposed to check to see if a file already exists, then if it does then leave it. If it doesn't, then replace it with a new file with a new name. It prompts the user to input the new file name as well. This is just what I copied from the community forum. The Highlighted sections show where the "Path" is located.
InvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim refDoc As Document
For Each refDoc in refDocs
'MessageBox.show(refDoc.DisplayName)
If refDoc.DisplayName = "shell:1" then
'This will find only "shell:1" and execute all the following code.
'If the refDoc.DisplayName is not "Shell:1" it'll jump to the red "End If" and proceed to check the next component.
'input box to enter new file name
NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name", refDoc.DisplayName)
'NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name", iProperties.Value(refDoc.DisplayName, "Project", "Part Number"))
'if the new file name is blank then use the same name
If NewFileName = "" Then
NewFileName = refDoc.DisplayName
End If
'if the user also types in the extesion then remove the extension.
If NewFileName.EndsWith(".ipt") = True Then
NewFileName = NewFileName.substring(0,NewFileName.length-4)
'MessageBox.show(NewFileName)
End If
'if there is a change in the name then do the following.
If refDoc.DisplayName <> NewFileName & ".ipt" Then
'check to see the file already exist. if it does then replace.
If System.IO.File.Exists(Path & NewFileName & ".ipt") Then
Component.Replace(refDoc.DisplayName, Path & NewFileName & ".ipt", True)
Else
Dim oldName As String = refDoc.DisplayName
'if it does not exist then recreate the file
refDoc.saveas(Path & NewFileName & ".ipt",False)
Call UpdateDrawing(Path, oldName, NewFileName)
End If
FileChange = True
End If
End If
Next
'Not sure if you need any of the following as it appears to be building new sub-assemblies - and you are only replacing a part.
'If you don't need it, comment it out using a ' at the beginning of each line.
' If FileChange = True Then
' NewAssemblyName = InputBox("What is the new file name for the assembly?", "New Assembly File Name",iProperties.Value("Project", "Part Number"))
' If NewAssemblyName <> "" Then
' Dim oldName As String = ThisDoc.FileName(True) 'include extension
' ThisDoc.Document.SaveAs(Path & NewAssemblyName & ".iam" , False)
' Call UpdateDrawing(Path, oldName, NewAssemblyName)
' End If
' End If
iLogicVb.UpdateWhenDone = True
End Sub 'I'm not sure about this line.
'It could exit the iLogic code at this point. I've only ever used it to end a sub-routine, but none has been defined.
'Someone who understands code better than me will tell you what it does!
'And this next part is to do with Drawings? Again, you can probably ditch this too.
' Private Sub UpdateDrawing (ByVal Path As String, ByVal oldName As String, ByVal newName As String)
' Dim oDestinationDoc As DrawingDocument
' Dim strFileExtension As String = oldName.substring(oldName.Length-4,4)
' oldName = oldName.substring(0,oldName.Length-4)
' Dim sFileName As String = Path & oldName & ".idw"
' Try
' If System.IO.File.Exists(sFileName) Then
' oDestinationDoc = ThisApplication.Documents.Open(sFileName)
' oDestinationDoc.saveas(Path & newName & ".idw",False)
' Dim oDocDescriptor As DocumentDescriptor
' oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)
' Dim oFileDescriptor As FileDescriptor
' oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor
' oFileDescriptor.ReplaceReference(Path & newName & strFileExtension)
' oDestinationDoc.Update()
' oDestinationDoc.Close
' End If
' Catch
' oDestinationDoc.Close
' MessageBox.Show("error")
' End Try
' End Sub
Solved! Go to Solution.
Path should be a variable containing the path of the file. I do not believe it is a keyword for VB.net, or VBA, though I am not certain on the VBA part.
From MSDN-File.Exists Method (String)-:
Dim curFile As String = "c:\temp\test.txt" Console.WriteLine(If(File.Exists(curFile), "File exists.", "File does not exist."))
Basically you need to set the Path variable to the "c:\temp\" text string (in context of the MSDN example).
Can't find what you're looking for? Ask the community or share your knowledge.