Solved

Set a range programmatically

Posted on 2013-05-15
4
240 Views
Last Modified: 2013-05-15
Dear Experts:

I would like to set a range in the Active Document that ...

spans from the second paragraph of the first page till the end of the penultimate paragraph of the page where the first manual page break (Strg + Enter) occurs.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
  • 2
  • 2
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39167994
Hi Andreas,

Should the range include or exclude the second paragraph?  Also is the penultimate paragraph on the page that is terminated by the page break, or on the following one?
0
 

Author Comment

by:AndreasHermle
ID: 39168027
Hi Graham,

thank you very much for your swift support:

... the range is to include the second paragraph

... the penultimate paragraph is on the page that is terminated by the manual page break


Andreas
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39168158
Here it is as a function
Function GetRange(doc As Word.Document) As Range
    Dim rng As Range
    Dim rng1 As Range
    
    Set rng = doc.Range
    With rng.Find
        .Text = "^m"
        If .Execute Then
            Set rng1 = rng.Duplicate
        End If
    End With
    rng1.Move wdParagraph, -2
    rng1.Expand wdParagraph
    rng1.Collapse wdCollapseStart
    
    Set rng = doc.Paragraphs.First.Range
    rng.Collapse wdCollapseEnd
    rng.End = rng1.Start
    Set GetRange = rng
End Function

Open in new window

You can use it like this:
GetRange(ActiveDocument).Select

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 39168302
Graham,

as always, I am truly impressed with your expertise.

Thank you very much for your swift and professional support.

Regards, Andreas
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now