how do I get word and paragraph based on character position?

F

Fil

Let say I want to know the word or the paragraph that contains the character
2043.
How do I call them?

--------------------------------
For the story I have a book of Italian cooking recipes (the absolute best
one that every italian grandma has on her shelves) in plain text that I am
trying to put in a nice format. The problem is that it is ~ 3000 pages long.
Luckily when I pasted the content of the txt file (a friend of mine sent me)
into word, I noticed that all formatting information hadn't been lost. I have
one recipe per page and paragraphs were preserved.
Based on what I see in the index, I want to link the index to the body of
the document. When the index will be properly linked to the text I will be
able to create back the proper structure of the document: chapter, sub
chapter, ... with a dedicated formatting for each of them.

Right now I am going through the index, grabing the title of each section
(basically I get rid of the spaces and page number), looking at all the
occurences of this title in the body of the occument and will select among
them the one which is at the beginning of a paragraph, hoping hard there's
only one that match this criterion. Once I found this occurence I will link
the item of my index to this occurence.
 
F

fumei via OfficeKB.com

I am not following what you mean by the Subject title.

"based on character position"????

And what do you mean it contains the character 2043. Do you mean a literal
string "2043"?

We do not have enough information to suggest the "best" solution, but here is
a possibility. Say you are in your index - plain text I assume. You put
your cursor in a index "title" - say, Gnocchi.

What do you want to do? From your post, it seems you want to start looking
through the document for instances of "Gnocchi", and make a decision Yes or
No, as to whether that found instance is where you want to be. this will do
that.

Dim strIn As String
Dim r As Range
Dim response

Selection.Expand unit:=wdParagraph
strIn = Left(Selection.Text, Len(Selection.Text) - 1)

Set r = ActiveDocument.Range
With r.Find
Do While .Execute(FindText:=strIn, Forward:=True) = True
r.Select
response = MsgBox("Is this the one?", vbYesNo)
If response = vbNo Then
r.Collapse wdCollapseEnd
Else
Exit Sub
End If
Loop
End With

What it does:

1. expands the Selection to the paragraph. This an assumption that may, or
may not, be correct. It could be adjusted. It is here so that you could put
the cursor in a line of multiple words, and get the whole line (or really,
paragraph)

2. sets a string variable to use as the search string - less the paragraph
mark

3. set a range variable for the document

4. search the range (the document content) for the search string

5. when found, select it. This moves the Selection (the cursor) to THAT
location.

6. display a message asking if this is where you want to be

7. if No, collapse the range and move on to the next found instance of the
search string

8. if Yes, Exit the procedure...as you have decided you are at the right
place.

Now, the code could certainly be adjusted to do something once you "are
there"...but you do not state exactly what that is.
 
T

Tony Strazzeri

For each entry in the index, discard the page number and trainling
spaces, then search for the index entry (text) and append the paagraph
mark character/indicator "^p" to the search criteria.

In the replace field enter "^&" this tells word to use the search
taget as the replace text. Additionally with mous ecurson in the
replace textt, click the format button and select style "Heading 1".

The effect of this is to find e the paragraph with the index entry and
replace it with itself but formatted as heading 1 style.


You can then regenerate the index from the heading 1 styles giving you
the page number where the item now appears.



Hope this helps.


Cheers
TonyS.
 
T

Tony Strazzeri

Sorry about the typos. I experienced one of those (rare) occurences
where the screen was unable to keep up with my typing.

Cheers
T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top