+ Reply to Thread
Results 1 to 10 of 10

Woring one character at a time (comparing)

  1. #1
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    33

    Woring one character at a time (comparing)

    Hi all,

    Was wondering why the failures trying to use document character positions...

    Please Login or Register  to view this content.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    2,742

    Re: Woring one character at a time (comparing)

    Since we don't know what curPos is, or what the range contains, it's impossible to advise on that. Likewise, without knowing what you're trying to achieve, it's impossible to advise whether there's a better way of achieving the same end.
    Cheers,
    Paul Edstein
    [MS MVP - Word]

  3. #3
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    33

    Re: Woring one character at a time (comparing)

    curPos is the character number where begins a bunch of paragraphs that match what was selected. curPos is correct, but only the code using .Range finds the correct place in the document.

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    2,742

    Re: Woring one character at a time (comparing)

    You still haven't provided anywhere near enough information...

  5. #5
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    33

    Re: Woring one character at a time (comparing)

    Quote Originally Posted by macropod View Post
    You still haven't provided anywhere near enough information...
    Quote Originally Posted by Grug.Crood View Post
    Hi all,

    Was wondering why the failures trying to use document character positions...

    Please Login or Register  to view this content.

    Select a range in a document, let mySelected.characters be that range.

    Read through the rest of the document a paragraph at a time, and if (statement) it finds the first paragraph matches mySelected's first paragraph;

    it found a match: This is where the above code resides...

    Compare the complete selection (not just that first paragraph).

    Issue is, the failed code isn't looking in the correct place, which is curPos, but the code "ActiveDocument.Range (curPos + i, curPos + i + 1)" is.

    Question is why don't the failed attempts look in the correct location?

  6. #6
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    2,742

    Re: Woring one character at a time (comparing)

    Given that the ranges you're comparing may contain tables, fields, inlineshapes and other content that cannot be processed as simple strings, you can't treat a document as nothing more than a plain text stream. Since you're wanting to employ paragraph-level comparisons, try something along the lines of:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    33

    Re: Woring one character at a time (comparing)

    @macropod

    I did "clear" the document of tables and shapes first, and I did not do as you did and assign an object variable to the document for the purposes of "extracting" each character. I've since (yesterday) also found out during testing that the code pulling the characters fails in the middle on some of the targeted strings and that made me suspect embedded special characters --- it just stops incrementing the range being compared.

    Is there something besides tables and shapes activedocument.characters(index) will choke on besides those?

    I see you coded "StrTxt = Selection.Paragraphs(1).Range.Text". Will .Text "filter" results of a range?

    It's for work, but I was going to play with it today, maybe peek at each character (some kind of .type() function?). I usually code in Excel, which is bombastic enough, but the Word object model is kinda like the plague in the 1600's, I can't imagine anyone willingly subjecting themselves to it, so, thanks for responding!!!!!!!!!!

    '
    Grug

  8. #8
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    2,742

    Re: Woring one character at a time (comparing)

    As I said, fields can also affect what you're doing. Referring to a range's .Text property will treat fields as if they'd been converted to text.

    As I also said, without knowing what you're trying to achieve, it's impossible to advise what might be a better way of going about it.

  9. #9
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    33

    Re: Woring one character at a time (comparing)

    Quote Originally Posted by macropod View Post
    As I said, fields can also affect what you're doing. Referring to a range's .Text property will treat fields as if they'd been converted to text.

    As I also said, without knowing what you're trying to achieve, it's impossible to advise what might be a better way of going about it.
    @macropod

    Got an hour to look back at it and was just going line-by-line after one small conversion of the activedocument.characters to an object variable ... size of the selected wasn't right and bingo, you nailed it on the head:

    Please Login or Register  to view this content.
    Apparently the two ways of looking at text:

    ' Dim mySelected As Range
    ' Set mySelected = Selection.Range
    mySelected.Characters (i + 1) = ActiveDocument.Range (curPos + i, curPos + i + 1)
    are not equivalent where it comes to treatment of fields. The first converted to text, the latter returned "". I am trying to avoid uploading the entire document (160 pages of text) so maybe next step is remove all fields from the document and retest.

    Thanks for reply, as I said, you nailed it.

    Are there any other caveats to reading one character at a time once tables, shapes, and (now) fields are eliminated?

    Thanks again!

    Grug

  10. #10
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    2,742

    Re: Woring one character at a time (comparing)

    Instead of your current approach, you really should learn to use the methods Word provides for handling ranges. Other content that might mess with your approach includes: footnote & endnote references, Section breaks, manual page breaks, line breaks, tabs, content controls, activex controls, etc., etc.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1