Selecting text to the right of a specific string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rugbykorn
    New Member
    • Mar 2012
    • 15

    Selecting text to the right of a specific string

    Hi,

    I am using some code I found online to find a text string in a PDF.

    Code:
     
    Private Sub Command3_Click()
    'IAC objects
    Dim gAvDoc As Object
    
    'variables
    Dim Resp 'For message box responses
    Dim gPDFPath As String
    Dim sText As String 'String to search for
    Dim sStr As String 'Message string
    Dim foundText As Integer 'Holds return value from "FindText" method
    
    'hard coding for a PDF to open, it can be changed when needed.
    gPDFPath = "C:\Documents and Settings\user\Desktop\testsearchpdf.pdf"
    
    'Initialize Acrobat by creating App object
    Set gApp = CreateObject("AcroExch.App")
    gApp.Hide
    
    'Set AVDoc object
    Set gAvDoc = CreateObject("AcroExch.AVDoc")
    
    ' open the PDF
    If gAvDoc.Open(gPDFPath, "") Then
    sText = "SD15"
    'FindText params: StringToSearchFor, caseSensitive (1 or 0), WholeWords (1 or 0), ResetSearchToBeginOfDocument (1 or 0)
    foundText = gAvDoc.FindText(sText, 1, 0, 1) 'Returns -1 if found, 0 otherwise
    
    Else
    ' if failed, show error message
    Resp = MsgBox("Cannot open" & gPDFPath, vbOKOnly)
    End If
    If foundText = -1 Then
    'compose a message
    sStr = "Found " & sText
    Resp = MsgBox(sStr, vbOKOnly)
    Else
    ' if failed, show error message
    Resp = MsgBox("Cannot find" & sText, vbOKOnly)
    End If
    gApp.Show
    gAvDoc.BringToFront
    End Sub
    What I need to do is select the text that is to the right of the text string, lets say 15 characters that appear to the right of the string. Any suggestions?

    Thanks,

    RugbyKorn
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Use the Right function.
    Code:
    Right("string", #)
    Where # is how many characters you want from the right.

    Comment

    • Rugbykorn
      New Member
      • Mar 2012
      • 15

      #3
      Hi Rabbit,

      I appreciate you taking the time to review my post. I was hoping it would be that simple but here is what I ran into. The code I pasted uses
      Code:
      gAvDoc.FindText("String", 1, 0, 1)
      to locate the string I want. However that function is simply a true false (0 or 1) as to whether the string I specify exists. What I need to do is search for the "string" and anything to the right of it, so something like
      Code:
       "String" & *
      . However I can't use the FindText function since that will simply tell me if the string exists, and not what is contained in the wildcard section of it.

      I hope this makes sense. I am more then happy to clarify anything I just wrote if you would like some more background on what I am trying to achieve.

      Thanks!

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Here is the Basic Logic for finding a Sub-String within a String, then outputting the 15 Characters after the Sub-String. The Code makes many Assumptions that I will not go into at this point.
        Code:
        Const conTEST_String As String = "AAAAA AA AAAAA  AAAAA  AAARDK12YE YOU FOUND ME  IGNORE  THER  EST^^&%OFTHIS"
        Const conSTING_TO_FIND As String = "RDK12"
        Dim intStartPos As String
        
        intStartPos = InStr(conTEST_String, conSTING_TO_FIND)
        
        If intStartPos = 0 Then Exit Sub
        
        Debug.Print Mid$(conTEST_String, intStartPos + Len(conSTING_TO_FIND), 15)
        OUTPUT (15 characters after Search String):
        Code:
        YE YOU FOUND ME
        Used within the Context of a Traditional Access Text Box:
        Code:
        Const conSTING_TO_FIND As String = "RDK12"
        Dim intStartPos As String
        
        intStartPos = InStr(Me![txtTest], conSTING_TO_FIND)
        
        If intStartPos = 0 Then Exit Sub
        
        With Me![txtTest]
          .SetFocus
          .SelStart = (intStartPos + Len(conSTING_TO_FIND)) - 1
            .SelLength = 15
        End With

        Comment

        • Seth Schrock
          Recognized Expert Specialist
          • Dec 2010
          • 2965

          #5
          @Rabbit I think that OP is wanting the characters TO the right not FROM the right, in which case the Mid() function would be needed. However, I don't see in the code provided how to get the string value. I only see that it says that it found the search string or not, but not where it would return the string found in the PDF.

          Comment

          • Rugbykorn
            New Member
            • Mar 2012
            • 15

            #6
            Hi Seth,

            Again, thank you everyone for taking a look at this as I would love to figure this out. At the bottom of the code a MsgBox is displayed showing the value that was searched for, but as you said its not displaying the actual value found. I know what I need to do, which is to search for a value, and identify the characters next to that value, say the 10 characters to the right of it. However I don't now how to index the whole PDF so that I can then use the inStr function as @ADezii suggested.

            RugbyKorn

            Comment

            • Rugbykorn
              New Member
              • Mar 2012
              • 15

              #7
              Hi @ADezii,

              Do you have any suggestions for how I can turn the entire contents of the PDF into a string so that I can then use the inStr function?

              RugbyKorn

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                Do you have any suggestions for how I can turn the entire contents of the PDF into a string so that I can then use the inStr function?
                I know of no such Method to accomplish this.

                Comment

                • Rugbykorn
                  New Member
                  • Mar 2012
                  • 15

                  #9
                  @ADezii,

                  I'm thinking that may be a dead end. I am now trying to do this with an XPS document instead of a PDF. I will open a new comment thread. Thanks for taking the time to read.

                  RugbyKorn

                  Comment

                  Working...