1

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 the highlighted 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)I have tried to use both text and binary comparison modes with the instr function

3) Ultimate goal: a)Extract highlighted emails from outlook and combine into one text file. b)Sort the delimited text file based on the text of the email (if text between delimiters contains y, put into array y, if x...etc.) c)print arrays into email in my specified order (e.g. 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
Dim xxxxx As String
xxxxx = xxxxx

'check each array index for contents
'note i have 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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Info5ek
  • 1,227
  • 4
  • 17
  • 25
  • 1
    Am I being dumb? - can't see `InStr` used in this code? – Jon Egerton Nov 05 '12 at 14:29
  • 1
    Also `xxxxx = xxxxx` is not the same as `xxxxx = "xxxxx"`. You're setting the variable to itself, so it'll be empty. – Jon Egerton Nov 05 '12 at 14:30
  • thanks for pointing out the self assignment error. However that is merely a remnant from my previous experiments with trying to compare the string and it isn't used in the broken if-then statement. Also, i have edited the post to make my overall goal clearer. – Info5ek Nov 05 '12 at 15:50
  • just a wild guess: in your call to the instr function you specify 'binary compare' (0 as arg #4). thus you actually compare octets as opposed to characters and thus might fall victim to different character representations under different encodings - e.g. the combination of utf-8 for file contents and iso-8859-1 for string literals could produce false positives and negatives alike. according to [comment 2 of this post's answer](http://stackoverflow.com/questions/2865613/how-to-define-a-string-literal-containing-non-ascii-characters), you can force utf8 in lits by including unicode-only chars. – collapsar Nov 05 '12 at 16:35
  • I have done both textual comparison and binary comparison and neither worked. What is in the message above is just the current version of the code. I have also posted my desired outcome if anyone is inclined to tweak and test various solutions and post results. – Info5ek Nov 05 '12 at 18:21

1 Answers1

3

Note that InStr returns an integer, so testing the result of the function as a boolean should result in inconsistent behavior. Try this:

If InStr(wrapArray(i),"xxxxx") > 0 Then
  MsgBox "FoundDelim! at line " & i
End If
Kevin Pope
  • 2,964
  • 5
  • 32
  • 47
  • Thanks, I have finally solved it by using the following line of code: `For i = LBound(wrapArray()) To UBound (wraparray()) If InStr(1, wrapArray(i), "xxx", vbTextCompare) <> 0 Then MsgBox "FoundDelimiter at line " & i End If Next i` – Info5ek Nov 05 '12 at 20:47
  • 1
    Excellent! If this answered your question, you can mark it as the answer by clicking the check mark to the left of the answer text. This can help you with your future inquiries by giving you a higher 'accepted answers' ratio, which some users check before answering questions. – Kevin Pope Nov 05 '12 at 21:48