PDA

View Full Version : Trouble parsing,searching text in array. False positives/Negatives



skyd171
11-05-2012, 07:58 AM
I am trying to look through an array for values using InStr. However, I am getting false positives and false negatives.

I.e. there will be a string match and no match will be reported or no match reported even though the string matches exactly.

It requires Outlook. It converts selected emails into a text file placed in the specified path. It then searches the text file for the delimeter by reading the text into an array and searching the array contents. However, as mentioned, even when the substring is in the array, InStr returns false positives and false negatives. I have tried binary and textual comparison methods and various comparison strings.
Any ideas?

Notes: 1)after becoming frustrated with InStr, I attempted to use an if then statement to compare the array to the delimeter string, which is what is shown in the last lines of my code.

2)i have set the delimeter as "xxxxx" which you can see in the lines following the set of objfile.write commands.

3) Ultimate goal:
a)Extract highlighted emails from outlook and combine into one text file.
b)Sort the text file based on the text of the email
b)generate new email that reprints the emails in order x, y, z,


this is because the emails arrive at different times each day, but must be organized into the same order each day for the final summary email.
Thanks.
Below is my code:







Sub
MergeTextFromSelectedEmailsIntoTextFile()





Dim objFS As New
Scripting.FileSystemObject, objFile As Scripting.TextStream





Dim objItem As Object,
strFile As String





Dim wrapArray() As
String





If
ActiveExplorer.Selection.Count = 0 Then Exit Sub





strFile = InputBox("Please enter the
full path and file name for the merged text:" _





, "Enter
File Name")












Set objFile =
objFS.CreateTextFile(strFile, False)











If objFile Is
Nothing Then





MsgBox
"Error creating file '"& strFile & "'.", vbOKOnly + vbExclamation
_






, "Invalid File"





Exit
Sub





End If





For Each objItem In ActiveExplorer.Selection





objFile.Write
vbCr



objFile.Write (vbCr & Chr(10) & Chr(13) & ("xxxxx") & Chr(13)
& Chr(13) & vbCr & vbLf)



objFile.Write (vbCr & vbLf & objItem.Body)



objFile.Write (vbCr & vbLf & Chr(13))






Next





objFile.Close





'Define variables for writing
to array


Dim i As Integer





Set objFile =
objFS.OpenTextFile(strFile, ForReading)





'write file contents to
array





Do Until
objFile.AtEndOfStream





ReDim Preserve
wrapArray(i)





wrapArray(i) =
objFile.ReadLine





i = i + 1





Loop





'check each array index for
contents


'note i have also tried


'if instr(0, wraparray(i), "xxxxx",0) then msgBox "FoundDelim at line " &
i





For i = LBound(wrapArray()) To
UBound(wrapArray())


If wrapArray(i) = "xxxxx" Then MsgBox "FoundDelim! at line " &
i

Next i

Crocus Crow
11-05-2012, 04:02 PM
It's easier to help you if you post the code correctly without all the extraneous lines and spaces. Anyway, I've tidied up your code and this works as required:
Sub MergeTextFromSelectedEmailsIntoTextFile()

Dim objFS As New Scripting.FileSystemObject, objFile As Scripting.TextStream
Dim objItem As Object, strFile As String
Dim wrapArray() As String
Dim delimiter As String

delimiter = "xxxxx"

If ActiveExplorer.Selection.Count = 0 Then Exit Sub

strFile = InputBox("Please enter the full path And file name For the merged text:", "Enter File Name")

Set objFile = objFS.CreateTextFile(strFile, True)
If objFile Is Nothing Then
MsgBox "Error creating file '" & strFile & "'.", vbOKOnly + vbExclamation, "Invalid File"
Exit Sub
End If

For Each objItem In ActiveExplorer.Selection
objFile.WriteLine delimiter
objFile.Write objItem.Body
Next

objFile.Close

'Define variables for writing To array

Dim i As Integer

Set objFile = objFS.OpenTextFile(strFile, ForReading)

'write file contents to array

Do Until objFile.AtEndOfStream
ReDim Preserve wrapArray(i)
wrapArray(i) = objFile.ReadLine
i = i + 1
Loop

'check each array index for contents

For i = LBound(wrapArray()) To UBound(wrapArray())
If InStr(wrapArray(i), delimiter) > 0 Then MsgBox "Found Delim! at line " & i

'This also works
'If wrapArray(i) = "xxxxx" Then MsgBox "Found Delim! at line " & i
Next i

End Sub