Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is it possible to find specific text in the middle of a data string, using field codes?

    I'm inserting paragraphs into a document depending on a list of items received in a bookmark.
    I seem to be able to use only one wildcard in the comparison expression, so I can find an item only at the beginning or end of the string:

    { if { = or ( { compare subject_list = "Health and Safety*" }, { compare subject_list = "*Health and Safety" } ) } = 1 "Inserted paragraph" }

    How would I pick out the second of three or more items in subject_list?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    You might need to nest the additional comparisons in the "false" clause. In other words:

    IF first_comparison "True Result" IF second_comparison "True Result" IF third_comparison "True Result" "False Result"

  3. #3
    New Lounger
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks.
    Actually my problem is how to recognize text in the middle of a string.

    I have a string, subject_list, such as
    "Building access,Health & Safety,Holidays"

    And I have a series of IF statements in the document:
    { if subject_list includes "Building access" insert ParagraphB } [else do nothing]
    { if subject_list includes "Health & Safety" insert ParagraphHS }
    { if subject_list includes "Holidays" insert ParagraphH }
    { if subject_list includes "Superannuation" insert ParagraphS }

    So I need help with the "if subject_list includes 'Health & Safety'" bit.

    Field codes seem to allow only one wild card.
    So I can say { if subject_list = "Health & Safety*" ...} [asterisk at end]
    Or { if subject_list = "*Health & Safety" ...} [asterisk at beginning]
    But I can't say { if subject_list = "*Health & Safety*" ...} [asterisks at beginning and end]

    And I can put two (but just two) comparisons together, with { if { = or ( { compare ... }, { compare ... } ) } = 1 ... }.
    Or I can use nested IFs for more comparisons.
    But I still can't get to the middle of the string.

    There doesn't seem to be formulas for LEFT( ), RIGHT( ) or MID( ).

    Am I missing something? How should I do it?

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi grahamwv,

    You're correct in that there is no LEFT, MID or RIGHT test you can use in field coding. Consequently, unless you know the position of the required text substring relative to one end or the other of the main string, there is no practical way to test for it with field coding. For example:
    {IF{REF MainString}= "My Text*" 1 0}
    returns 1 if the substring begins with 'My Text', and
    {IF{REF MainString}= "???My Text*" 1 0}
    returns 1 if the substring begins with 3 characters followed by 'My Text'

    However, even where you can use a test like the above, you can't then manipulate the main string via field coding.

    Parsing numeric strings and dates/times is also feasible.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    New Lounger
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that, Macropod.
    It made me realize that I can use a number of question-mark wildcards as well as the asterisk.

    I experimented, and I came across a very weird effect.


    If I start with a subject_list of
    "Holidays,Building access,Health & Safety"

    I can then use a simple IF statement:
    { IF subject_list = "????????????????*Building access*" "inserted BA paragraph" }

    Just that.
    The trick is to have one more question-mark than the string I'm specifying, plus the asterisk.
    And note that I now have two asterisks.
    Note also that there are NOT sixteen characters before the start of the string I'm searching for.

    If I want to find the first item in the string, it's the same construction:
    { IF subject_list = "?????????*Holidays*" "inserted paragraph" }

    The number of question-marks is related to the length of the string I'm searching for, not the one I'm searching in.


    Is that weird, or what?
    Is there a reason for this, and is it something that I can continue to rely upon?

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by grahamwv View Post
    Thanks for that, Macropod.
    It made me realize that I can use a number of question-mark wildcards as well as the asterisk.

    I experimented, and I came across a very weird effect.


    If I start with a subject_list of
    "Holidays,Building access,Health & Safety"

    I can then use a simple IF statement:
    { IF subject_list = "????????????????*Building access*" "inserted BA paragraph" }

    Just that.
    The trick is to have one more question-mark than the string I'm specifying, plus the asterisk.
    And note that I now have two asterisks.
    Note also that there are NOT sixteen characters before the start of the string I'm searching for.

    If I want to find the first item in the string, it's the same construction:
    { IF subject_list = "?????????*Holidays*" "inserted paragraph" }

    The number of question-marks is related to the length of the string I'm searching for, not the one I'm searching in.


    Is that weird, or what?
    Is there a reason for this, and is it something that I can continue to rely upon?
    Hi grahamwv,

    Unfortunately, you can't rely on that approach. Your field code returns a true result for any sequence of '?' marks as long as or longer than the sub string.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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