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
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