Hi all,
Was wondering why the failures trying to use document character positions...
Please Login or Register to view this content.
Hi all,
Was wondering why the failures trying to use document character positions...
Please Login or Register to view this content.
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
[Fmr MS MVP - Word]
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.
You still haven't provided anywhere near enough information...
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?
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.
@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
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:
Apparently the two ways of looking at text:Please Login or Register to view this content.
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.' Dim mySelected As Range
' Set mySelected = Selection.Range
mySelected.Characters (i + 1) = ActiveDocument.Range (curPos + i, curPos + i + 1)
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
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks