|
|
|
date: Fri, 15 Feb 2008 09:05:01 -0800,
group: microsoft.public.word.vba.beginners
back
Re: how do I get word and paragraph based on character position?
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.
Fil wrote:
>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.
>--------------------------------
>
>Thx
--
Message posted via http://www.officekb.com
date: Fri, 15 Feb 2008 21:21:02 GMT
author: fumei via OfficeKB.com u37563@uwe
|
|