Solved

Set a range programmatically

Posted on 2013-05-15
4
263 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

773 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