MS Office Forum / Word / Programming / April 2007
Can't select text and bookmark it
|
|
Thread rating:  |
NavyPsych - 05 Apr 2007 23:35 GMT Okay, I'm very new at this and this is probably very easy, but here goes. All I want to do is go into my footer, which contains a few lines, e.g. NAME: Doe, John, then SSN:....you get the picture. So I found this subroutine that does that works perfectly (selects name and bookmarks it), but when I repeat the subroutine to grab and bookmark the SSN, it doesn't work - the it makes the end of my NAME book mark turn from an end bracket to the bookmark that looks like a big "I" So, I experiemented at made rgn2 look for the last number in the social, e.g. "8" instead of a paragraph marker, and it works (but it of course doesn't select the "8"
So, my dirty fix is to add a $ to the end to the SSN line, then have it find that (b/c it's before the ^p), and it grabs the whole social. Anyway, I don't like having to cheat...I'd like to know why this won't work. My theory is that it's finding the paragraph marker from the first line again, but I don't know how to have it find the ^p on the second line instead. Help!
Sub SelectText() 'Grab NAME ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter Selection.WholeStory Set rng1 = Selection.Range Set rng2 = rng1.Duplicate With rng1.Find .ClearFormatting .Text = "Name:^w" .Execute If .Found Then With rng2.Find .Text = "^p" .Execute If .Found Then rng1.Collapse wdCollapseEnd rng1.End = rng2.Start rng1.Select End If End With End If End With 'Add Bookmark With ActiveDocument.Bookmarks .Add Range:=Selection.Range, Name:="NAME" .DefaultSorting = wdSortByName .ShowHidden = False End With '
Jean-Guy Marcil - 06 Apr 2007 03:48 GMT NavyPsych was telling us: NavyPsych nous racontait que :
> Okay, I'm very new at this and this is probably very easy, but here > goes. All I want to do is go into my footer, which contains a few [quoted text clipped - 13 lines] > first line again, but I don't know how to have it find the ^p on the > second line instead. Help! I find it difficult to understand what you have and what you want to do with it...
How many bookmark do you need? Can you show us the Before and After so we get a clear picture how the text is laid out. If there is more than one line, how are the line breaks created? (Enter or Shift-Enter) Is this text in the first section's footer? What type of footer is it (First, Regular, Odd/Even)
I am asking all this because with those answer we can provide a routine that will be made of just a few lines of code. Search should not be necessary... unless the pattern is unpredictable.
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
NavyPsych - 06 Apr 2007 06:56 GMT > NavyPsych was telling us: > NavyPsych nous racontait que : [quoted text clipped - 39 lines] > jmarcilREM...@CAPSsympatico.caTHISTOO > Word MVP site:http://www.word.mvps.org BEFORE: A regular footer that appears as follows.
NAME: Doe, Jane SSN: 00/123-45-6789 STATUS: Active Duty Navy E4 DOB: 01 JAN 1980
I only want to bookmark the Name "Doe, Jane," and the SSN "00/123-45-6789" I am using this bookmarks to place this information in a table in the active document. The line breaks are regular breaks, i.e. a paragraph marker (¶) following each line. BTW, after bookmarking is done, I need to delete everything below the first two lines and leave the NAME and SSN lines intact.
AFTER: So this is a table that appears on the first page of the docuement, and have reference fields there display the bookmarks once I can capture them.
NAME: Doe, Jane RATE/RANK/SVC: SSN: 00/123-45-6789 WARD 5 [__] ADM DATE: [_____] DIS DATE: [_____] COMMAND: N/A
Thanks in advance for all your help -- Kevin
Doug Robbins - Word MVP - 06 Apr 2007 13:12 GMT Use the following:
Dim myrange As Range Selection.HomeKey wdStory Selection.Find.ClearFormatting With Selection.Find Do While .Execute(findText:="NAME", Forward:=True, Wrap:=wdFindStop) = True Set myrange = Selection.Paragraphs(1).Range myrange.Start = myrange.Start + 6 myrange.End = myrange.End - 1 ActiveDocument.Bookmarks.Add Name:="Name", Range:=myrange Loop End With Selection.HomeKey wdStory With Selection.Find Do While .Execute(findText:="SSN", Forward:=True, Wrap:=wdFindStop) = True Set myrange = Selection.Paragraphs(1).Range myrange.Start = myrange.Start + 5 myrange.End = myrange.End - 1 ActiveDocument.Bookmarks.Add Name:="SSN", Range:=myrange Loop End With Selection.HomeKey wdStory With Selection.Find Do While .Execute(findText:="STATUS", Forward:=True, Wrap:=wdFindStop) = True Selection.Paragraphs(1).Range.Delete Loop End With Selection.HomeKey wdStory With Selection.Find Do While .Execute(findText:="DOB", Forward:=True, Wrap:=wdFindStop) = True Selection.Paragraphs(1).Range.Delete Loop End With
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
On Apr 5, 10:48 pm, "Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote:
> NavyPsych was telling us: > NavyPsych nous racontait que : [quoted text clipped - 42 lines] > jmarcilREM...@CAPSsympatico.caTHISTOO > Word MVP site:http://www.word.mvps.org BEFORE: A regular footer that appears as follows.
NAME: Doe, Jane SSN: 00/123-45-6789 STATUS: Active Duty Navy E4 DOB: 01 JAN 1980
I only want to bookmark the Name "Doe, Jane," and the SSN "00/123-45-6789" I am using this bookmarks to place this information in a table in the active document. The line breaks are regular breaks, i.e. a paragraph marker (¶) following each line. BTW, after bookmarking is done, I need to delete everything below the first two lines and leave the NAME and SSN lines intact.
AFTER: So this is a table that appears on the first page of the docuement, and have reference fields there display the bookmarks once I can capture them.
NAME: Doe, Jane RATE/RANK/SVC: SSN: 00/123-45-6789 WARD 5 [__] ADM DATE: [_____] DIS DATE: [_____] COMMAND: N/A
Thanks in advance for all your help -- Kevin
Jean-Guy Marcil - 06 Apr 2007 16:02 GMT NavyPsych was telling us: NavyPsych nous racontait que :
>> NavyPsych was telling us: >> NavyPsych nous racontait que : [quoted text clipped - 65 lines] > > Thanks in advance for all your help -- Kevin Try this:
'_______________________________________ Sub CreateBookamrks() Const strName As String = "bkm_Name" Const strSSN As String = "bkm_SSN" Dim rgeName As Range Dim rgeSSN As Range
Set rgeName = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) _ .Range.Paragraphs(1).Range With rgeName .MoveStart wdCharacter, 6 .MoveEnd wdCharacter, -1 End With Set rgeSSN = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) _ .Range.Paragraphs(2).Range With rgeSSN .MoveStart wdCharacter, 5 .MoveEnd wdCharacter, -1 End With
With ActiveDocument .Bookmarks.Add strName, rgeName .Bookmarks.Add strSSN, rgeSSN .Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs(3).Range.Delete .Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs(3).Range.Delete End With
End Sub '_______________________________________
You may have to change "Sections(1).Footers(wdHeaderFooterPrimary)" depending on the actual footer that contains this stuff (You did not answer this part of my questions, so I assumed that the information is in the first standard footer).
(More than a few lines because you want to add two bookmarks and delete two paragraphs...!)
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
NavyPsych - 06 Apr 2007 22:24 GMT Thank you very much. I haven't used it yet, but it's so "cleanly" written even I, as a novice, can tell what you've done and that it is going to work. My only question: what if the authors of these documents (and there are many) use varying amounts of spaces after the colons (or using tabs)? That was my rationale for using the ^w wildcard. Anyway, thanks a ton. BTW, could you tell me why my script would work the first go-round (capturing the name as a bookmark), whereas the second run would grab the paragraph marker with the SSN? It drove me crazy for days, and I just have to know why. Thanks again.
> NavyPsych was telling us: > NavyPsych nous racontait que : [quoted text clipped - 116 lines] > jmarcilREM...@CAPSsympatico.caTHISTOO > Word MVP site:http://www.word.mvps.org Jean-Guy Marcil - 07 Apr 2007 02:56 GMT NavyPsych was telling us: NavyPsych nous racontait que :
> Thank you very much. I haven't used it yet, but it's so "cleanly" > written even I, as a novice, can tell what you've done and that it is [quoted text clipped - 6 lines] > It drove me crazy for days, and I just have to know why. Thanks > again. You are going to have to add code to test the first and last characters and remove them from the range while they are equal to non acceptable characters.
As for your code, I am sorry, I cannot answer your question. When I saw it, coupled with the fact that I was not clear on what you wanted to do, I did not really examine it or test it. Also, I do not see anything regarding the SSN in the code you posted...
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
NavyPsych - 09 Apr 2007 15:57 GMT Thanks again, all. I actually solved some of my problems (regarding the irregularity of space/tabs after "NAME:" etc. I did a find "NAME:^w" and replaced with "NAME: " - and then the numbers 6 and -1 worked like a charm. So, just when I thought all was going well, somebody (!) puts their footer text starting on line two (so there's a paragraph marker in in the first line).
So, I'm thinking, this has to be easy. Some IF...THEN statement that will delete the first line (actually a paragraph) if it's empty, then everything will be back in their respective lines (i.e. "NAME:" in Paragraph(1), "SSN: " in Paragraph(2).
This is easy, right?
Jean-Guy Marcil - 09 Apr 2007 20:20 GMT NavyPsych was telling us: NavyPsych nous racontait que :
> Thanks again, all. I actually solved some of my problems (regarding > the irregularity of space/tabs after "NAME:" etc. I did a find [quoted text clipped - 9 lines] > > This is easy, right? If you have unpredictable text that you have to process, I am afraid it will not be easy, and ultimately nearly impossible. If the content is left to user input, you are going to get all kinds of scenarios so that any macro will not be able to handle them all. So far, since you cannot even guarantee paragraph position within the footer, your best bet is Doug's macro with the series of "Find". But even that may fail depending on what users have done.
I think you have to find a way to implement some kind of scenario where the content will be user proof.
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
|
|
|