#1
|
|||
|
|||
How to Select text between Chapters
Hello,
I need to select (and later on copy to other document) all the content between the Chapters 1.2 and 1.3. I've tried this but need some help to fix this, can someone help me on this? Thanks! Sub test() 'Select tekst between Chapter 1.2 and 1.3 Set WDoc = ActiveDocument For Each docfield In WDoc.Fields Select Case docfield.Type Case 88 Set objRange = docfield.Result 'Set Start en end for selection If Left(objRange.Text, 3) = "1.2" Then Set TStart = objRange.Range If Left(objRange.Text, 3) = "1.3" Then Set TEnd = objRange.Range End Select Next WDoc.Range(TStart.Start, TEnd.End).Select MsgBox "Done" Set WDoc = Nothing End Sub |
#2
|
|||
|
|||
Your questions isn't very clear.
What do you mean by text between chapters? You code appears to be searching for hyperlink fields (wdfieldtypehyperlink has the value of 88) and is selecting text between (and including) two hyperlink fields. Can you show an example of what you are trying to do or upload a copy of the document. |
#3
|
||||
|
||||
If the heading for Chapters 1.2 & 1.3 use Heading Styles, you might try something based on:
Selection.GoTo What:=wdGoToBookmark, Name:="\HeadingLevel" This will expand the selection to include the heading that contains the insertion point or selection, plus any subordinate headings and text. If the current selection is body text, the "\HeadingLevel" bookmark includes the preceding heading, plus any headings and text subordinate to that heading.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Thanks Slaycock and Macropod!
This explanes a lot :-) The way to go (I think) is to find the correct header text and select the content between the headers. I Know that the name of the headers are away the same ;-) |
#5
|
||||
|
||||
If the heading for Chapters 1.2 & 1.3 use Heading Styles, you can use Find to locate the heading, then .GoTo What:=wdGoToBookmark, Name:="\HeadingLevel" to get the corresponging content. For example:
Code:
Sub Demo() Application.ScreenUpdating = False Dim Rng As Range With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "Heading Text" .Style = "Heading 1" .Format = True .Forward = True .MatchCase = True .Wrap = wdFindStop .MatchWildcards = False .Execute End With If .Find.Found = True Then Set Rng = .Duplicate.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel") MsgBox Rng.Text End If End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Hello Macropod,
Thanks again, this is the way to go. I now can make the selection between the headers. But this selection include the first header :-( How to start the selection after the first find header? Thanks for support! |
#7
|
||||
|
||||
I'd have thought that was fairly obvious - after defining the heading range, contract the range to start at the end of the heading paragraph:
Rng.Start = Rng.Paragraphs.First.Range.End
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Hello Macropod,
I Still can't select after the find header :-( And I don't see why. Can you help me with debug the code? Thanks! Sub Select_text() Selection.WholeStory Selection.Collapse wdCollapseStart Selection.Find.ClearFormatting Selection.Find.Style = ActiveDocument.Styles("Kop 2") 'This will only search in header type "Kop 2" With Selection.Find .Text = "1.2 Doelstelling" 'Search for the first header text .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = True End With Selection.Find.Execute Selection.Collapse wdCollapseStart Dim R_start As Range Set R_start = Selection.Range 'Start Selection form first header text Selection.Find.Text = "1.3 Scenario's" 'Search for the second header text If Selection.Find.Execute Then Selection.Collapse wdCollapseStart Else Selection.WholeStory Selection.Collapse wdCollapseEnd End If Dim R_end As Range Set R_end = ActiveDocument.Range(R_start.End, Selection.Start) R_end.Select End Sub |
#9
|
||||
|
||||
I have to wonder why you aren't using the code I posted, with just the necessary detail changes... Seems I wasted the effort.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Sorry Macropod!
Based on your information you gave me before that (use Selection.GoTo) I got the idea to search the text. By get this to work, I didn't tray your approach. Now I understand that that's the way to go.. I will inform you tomorrow on my findings. Thanks again! |
#11
|
|||
|
|||
Hello Paul, sorry for highjacking this thread, but where does the line
Code:
Rng.Start = Rng.Paragraphs.First.End Code:
If .Find.Found = True Then Set Rng = .Duplicate Rng.Start = Rng.Paragraphs.First.End Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel") MsgBox Rng.Text End If If I comment out the line, the message box shows the text including the heading. I'm using Word 2016 via O365. |
#12
|
|||
|
|||
I found it. The way it works for me is
Code:
If .Find.Found = True Then Set Rng = .Duplicate Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel") Rng.Start = Rng.Paragraphs.First.Range.End MsgBox Rng.Text End If |
Tags |
chapters, selecting text |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
After putting a text box behind text I can't select it anymore | Mike84bs | Word | 2 | 07-25-2016 07:36 AM |
How to number chapters and sub-chapters in a few clicks? | Icelandic_Boy | Word | 1 | 05-24-2016 11:33 AM |
VBA Search Table for Text/Select Text/Insert Hyperlink | sldrellich | Word VBA | 3 | 03-24-2015 01:09 PM |
Microsoft Word macro to find text, select all text between brackets, and delete | helal1990 | Word VBA | 4 | 02-05-2015 03:52 PM |
Select section of text and change text | newbieX | Word VBA | 3 | 03-28-2014 04:21 PM |