Hi
I am trying to write code to collect standard info from a csv file to be added to iProperties.
Following is the code I have so far.
Dim Separators() As Char = {"\"c} Words = ThisDoc.Path.Split(Separators) FilName = "C:\Vault\Designs\" & Words(3) & "\Spec_Sheet.csv"
Dim ReadCSV As New System.IO.StreamReader(FilName) Dim WordSets As New ArrayList()
Do While ReadCSV.Peek <> -1 WordSets.Add(ReadCSV.ReadLine.Split(",")) Loop
i = 0 SName = iProperties.Value("Project", "Stock Number")
For Each wrd In WordSets If WordSets.item(i)(0) = SName Then iProperties.Value("Project", "Description") = WordSets.item(i)(1) iProperties.Value("Custom", "Finish") = WordSets.item(i)(2) End If i += 1 Next
The only problem I have is that this line crashes. I am not competent enough to understand why. Can anyone help me
Dim ReadCSV As New System.IO.StreamReader(FilName)
Anyone......
Solved! Go to Solution.
Solved by Mike.Wohletz. Go to Solution.
I think a little error handling could help to solve your problem. Please see the included on a little more eligant way to handle this.
Private Sub test() Dim Thisdoc As Document = ThisAppliocation.ActiveDocument Dim FilName As String = GetDirFromPath(Thisdoc.FullDocumentName) & "Spec_Sheet.csv" If My.Computer.FileSystem.FileExists(FilName) Then Dim WordSets As New ArrayList() Using ReadCSV As System.IO.StreamReader = New System.IO.StreamReader(FilName) Do While ReadCSV.Peek <> -1 WordSets.Add(ReadCSV.ReadLine.Split(",")) Loop ' work with your word sets in this area End Using Else 'FilName file did not exist. deal with this in this section or ignor it. MsgBox(String.Format("{0} does not exist", FilName), MsgBoxStyle.Information, "Error Reading File") End If End Sub Public Function GetDirFromPath(ByVal path As String) _ As String Try Return path.Substring(0, path.LastIndexOf("\") + 1) Catch ex As Exception ' error Return "" End Try End Function
Hi Mike
I have updated my code with your help. It looks like this and works.
DimSeparators()AsChar= {"\"c} Words=ThisDoc.Path.Split(Separators)DimFilNameAsString="C:\Vault\Designs\"&Words(3)&"\Spec_Sheet.csv"
IfMy.Computer.FileSystem.FileExists(FilName)ThenDimWordSetsAsNewArrayList()UsingReadCSVAsSystem.IO.StreamReader=NewSystem.IO.StreamReader(FilName)DoWhileReadCSV.Peek<>-1WordSets.Add(ReadCSV.ReadLine.Split(","))Loop
i=0SName=iProperties.Value("Project", "Stock Number")ForEachwrdInWordSetsIfWordSets.item(i)(0)=SNameTheniProperties.Value("Project", "Description")=WordSets.item(i)(1)iProperties.Value("Custom", "Finish")=WordSets.item(i)(2)EndIfi+=1NextEndUsingElse'FilName file did not exist. deal with this in this section or ignor it.MsgBox(String.Format("{0} does not exist", FilName), MsgBoxStyle.Information, "Error Reading File")EndIf
But I am still open to your suggestion of directory handling as outlined in my first reply to you.
Thank you