Results 1 to 11 of 11
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    my Instr code isnt working

    Hey there

    im using the syntax InStr to deteremine if the string im looking for is in the current variable string. heres my code

    the strTmpDocVarUpper data value is "DocVariable MC1MMASTER1 "
    the strPreviousDocVar data value is "MC1MMASTER1 "

    the issue is if "DocVariable MC1MMASTER1 " , "MC1MMASTER1 "
    returns false 1st time, i then amend the field.

    therefore the code checks the field again - it comes to the same field & test

    if "DocVariable MC1MMASTER1 " , "MC1MMASTER1 "
    however it keeps returning false

    is using Instr OK or should I be using other syntaxt to test thats more reliable?


    If InStr(strTmpDocVarUpper, strPreviousDocVar) = 0 Then
    msgbox "true"
    Else
    msgbox "false"
    Endif

    diana

  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: my Instr code isnt working

    It is impossible to tell from that little bit of code what the problem might be. My first suggestion would be to check VERY closely to make sure that the variable names are spelled correctly (no zeros for o's or ones for i's, etc.). You could also copy the variable names from the IF statement and paste them into a MsgBox statement to see what the problem might be. Be sure to copy and paste the variable names so that what is in the MsgBox is exactly the same as what is in the IF.
    Legare Coleman

  3. #3
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: my Instr code isnt working

    I think you're mixing up InStr and StrComp. InStr returns the position of the target string in the full string. StrComp, on the other hand, returns 0 if 2 strings are the same. To use InStr to test if StringFull contains strTarget, you'd use

    If InStr(strFull, strTarget) > 0 Then...

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: my Instr code isnt working

    You say you amend the field, but you aren't testing a field, you're testing two variables, or rather, testing whatever is contained in those two variables. How do you populate the variables and how do you update them after you have changed the field?
    Charlotte

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: my Instr code isnt working

    Instr will return zero if one string isn't found in the other, so it is equally valid to test for = 0.
    Charlotte

  6. #6
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: my Instr code isnt working

    Right, but she was evaluating a 0 result as True, which is why I suspected she was thinking of StrComp.

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: my Instr code isnt working

    Actually, as I read it, she is checking to see if the Instr returns a zero, that is, if the second string does NOT occur in the first. If so, she is returning a True, otherwise, a False. That is consistent with the stated result that it was always returning a false, since the second string was found in the first, so Instr would never return a zero. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  8. #8
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: my Instr code isnt working

    Hi

    thanks for what i would like my code to do:
    'check all fieldcodes in the doc
    'for the current fieldcode - if the fieldcode type is a docVariable then
    'obtain the DocVariable name
    'insert my 'skeleton fieldcode
    'in my 'skeleton' fieldcode inserted type the orginial DocVar name
    check the next field

    sub main
    If IsIDealDocsDocument = True Then
    Call CheckFields
    End If
    end sub

    Sub CheckFields()

    Dim fldThis As Field
    Dim strTmpDocVar As String
    Dim lngLen As Long
    Dim strNewDocVar As String
    Dim strPreviousDocVar As String
    Dim strDocNameNew As String
    Dim strDocName As String
    Dim strTmpDocVarUpper As String
    Dim myrange As Range

    ActiveDocument.ActiveWindow.View.ShowFieldCodes = True

    Dim afield As Field
    For Each afield In ActiveDocument.Fields
    If afield.Type = wdFieldDocVariable Then

    MsgBox afield.Code
    strTmpDocVar = afield.Code
    strTmpDocVarUpper = UCase(strTmpDocVar)
    MsgBox afield.Code & strPreviousDocVar

    If InStr(1, strTmpDocVarUpper, strPreviousDocVar, vbTextCompare) > 0 Then
    MsgBox "false"

    afield.Select

    'get the doc var name
    Dim lStartPos As Long
    UCase (strTmpDocVar)
    lStartPos = InStr(strTmpDocVar, "DOCVARIABLE")

    lngLen = Len(strTmpDocVar)
    strNewDocVar = Right(strTmpDocVar, lngLen - 12)

    strPreviousDocVar = UCase(strNewDocVar)

    afield.Delete

    InsertGlobalAutotext "DocVarIDD"

    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend

    Set myrange = Selection.Range

    With myrange.Find
    .ClearFormatting
    .Text = "<>"
    With .Replacement
    .ClearFormatting
    .Text = strNewDocVar
    End With
    .Execute Format:=True, Replace:=wdReplaceAll

    Selection.MoveRight Unit:=wdCharacter, Count:=1

    Set myrange = Nothing
    End With

    Else
    End If
    End If
    Next afield

    ActiveDocument.ActiveWindow.View.ShowFieldCodes = True


    the autotext entry i insert is a field. pls see the screen capture

    What im finding with the code is it picks up & edits the 1st docVariable field.
    The code then goes to the next field. The next field recognised is the autotext entry field Ive just amended - which is the orginial field however ive just amended the fieldcode.
    due to the code doing this -- i then added code to compare the DocVariable names. If there DocVariable names are the same then move & check the next field.
    however its not functioning correctly :-0

    i was thinking of amending the code to :
    check each field
    if current fieldtype is a DocVariable - then add to an array
    next field

    For items in the array
    goto that DocVariable field - update the fieldcode
    next item in the array

  9. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,803
    Thanks
    4
    Thanked 235 Times in 217 Posts

    Re: my Instr code isnt working

    Diana

    I think you will need to run the loop in reverse because the processing you are doing is changing the number of fields in the document and you will rapidly get caught in a terminal loop building a deeper and deeper nest. The aircode for what you are trying to achieve would look like this...

    Dim afield As Field, iCount as Integer
    iCount = ActiveDocuments.Fields
    For iCount = ActiveDocument.Fields.Count to 1 step -1
    afield = ActiveDocument.Fields(iCount)
    'code required to check that this field isnt already nested
    'your code which builds the nest goes here
    Next iCount
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: my Instr code isnt working

    In yesterday's post you were interpreting an InStr(...) = 0 result as True, and in today's post you're interpreting an InStr(...) > 0 result as False, which looks like the same issue in reverse.

    I'm probably misunderstanding, but if you only want the bulk of your code to run if strPreviousDocVar is not in strTmpDocVarUpper, it looks to me like your InStr(...) > 0 test should be changed to InStr(...) = 0.

  11. #11
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: my Instr code isnt working

    HI Andrew

    Thanks for your help. using your code & making a few amendments I managed to get it working .
    many thanks again...diana :-)


    ActiveDocument.ActiveWindow.View.ShowFieldCodes = True

    lngCount = ActiveDocument.Fields.Count

    ReDim arrField(lngCount) As String
    For Each afield In ActiveDocument.Fields
    If afield.Type = wdFieldDocVariable Then
    Set myrange = afield.Code
    ReDim Preserve arrField(x) As String
    arrField(x) = myrange
    x = x + 1
    End If
    Next afield


    Set myrange = Nothing
    lngCount = x
    x = 0
    For x = 0 To lngCount
    'place my other code in here to format the field
    next

    ActiveDocument.ActiveWindow.View.ShowFieldCodes = False

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •