Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-10-2017, 07:56 AM
edwinm edwinm is offline How to Select text between Chapters Windows 10 How to Select text between Chapters Office 2016
Novice
How to Select text between Chapters
 
Join Date: Aug 2017
Posts: 9
edwinm is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 10-10-2017, 08:36 AM
slaycock slaycock is offline How to Select text between Chapters Windows 7 64bit How to Select text between Chapters Office 2013
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 10-10-2017, 12:10 PM
macropod's Avatar
macropod macropod is offline How to Select text between Chapters Windows 7 64bit How to Select text between Chapters Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #4  
Old 10-10-2017, 10:10 PM
edwinm edwinm is offline How to Select text between Chapters Windows 10 How to Select text between Chapters Office 2016
Novice
How to Select text between Chapters
 
Join Date: Aug 2017
Posts: 9
edwinm is on a distinguished road
Default

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 ;-)
Reply With Quote
  #5  
Old 10-10-2017, 10:18 PM
macropod's Avatar
macropod macropod is offline How to Select text between Chapters Windows 7 64bit How to Select text between Chapters Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #6  
Old 10-10-2017, 11:01 PM
edwinm edwinm is offline How to Select text between Chapters Windows 10 How to Select text between Chapters Office 2016
Novice
How to Select text between Chapters
 
Join Date: Aug 2017
Posts: 9
edwinm is on a distinguished road
Default

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!
Reply With Quote
  #7  
Old 10-10-2017, 11:03 PM
macropod's Avatar
macropod macropod is offline How to Select text between Chapters Windows 7 64bit How to Select text between Chapters Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #8  
Old 10-10-2017, 11:28 PM
edwinm edwinm is offline How to Select text between Chapters Windows 10 How to Select text between Chapters Office 2016
Novice
How to Select text between Chapters
 
Join Date: Aug 2017
Posts: 9
edwinm is on a distinguished road
Default

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
Reply With Quote
  #9  
Old 10-11-2017, 12:17 AM
macropod's Avatar
macropod macropod is offline How to Select text between Chapters Windows 7 64bit How to Select text between Chapters Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #10  
Old 10-11-2017, 10:16 AM
edwinm edwinm is offline How to Select text between Chapters Windows 10 How to Select text between Chapters Office 2016
Novice
How to Select text between Chapters
 
Join Date: Aug 2017
Posts: 9
edwinm is on a distinguished road
Default

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!
Reply With Quote
  #11  
Old 04-03-2018, 09:15 PM
teylyn teylyn is offline How to Select text between Chapters Windows XP How to Select text between Chapters Office 2016
Novice
 
Join Date: Nov 2010
Posts: 2
teylyn is on a distinguished road
Default

Hello Paul, sorry for highjacking this thread, but where does the line

Code:
Rng.Start = Rng.Paragraphs.First.End
need to go? I tried it like this

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
and get the error "Object does not support this property or method".

If I comment out the line, the message box shows the text including the heading. I'm using Word 2016 via O365.
Reply With Quote
  #12  
Old 04-03-2018, 09:30 PM
teylyn teylyn is offline How to Select text between Chapters Windows XP How to Select text between Chapters Office 2016
Novice
 
Join Date: Nov 2010
Posts: 2
teylyn is on a distinguished road
Default

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
The crux was the Rng.Paragraphs.First.Range.End
Reply With Quote
Reply

Tags
chapters, selecting text



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Select text between Chapters 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
How to Select text between Chapters Select section of text and change text newbieX Word VBA 3 03-28-2014 04:21 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:33 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft