Using Find with a Range?

C

cjg.groups

Hello, I have the following code using a Selection, but is it possible
to use a Range?

I am using VBA to delete 32 unwanted blank lines before the text
"Table 11:" in a document. I can't use an absolute Range.Paragraph
reference because the amount of paragraphs above this point may vary.

Ideally, I would create a Range variable whose Start was "one line
after Tables(10)" and end was "one line before the text 'Table 11:'".
Is that a possible alternative?

With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.Execute FindText:="Table 11:"
End With
With Selection
.Collapse
For i = 1 To 32
.TypeBackspace
Next i
.InsertBreak Type:=wdPageBreak
End With


And is there a way to return the selection to the top of the document
after this operation?

Thank you for your help?
 
L

Lene Fredborg

Last question first: to return to the top of the document, you can use:

Selection.HomeKey (wdStory)

I can see that your code inserts a page break. If the purpose is to make
“Table 11:†start on a new page, it is much better to apply PageBreakBefore
to that paragraph. Manual page breaks may, for example, result in empty pages
if you change the content before the break.


Deletion of text between tables 10 and “Table 11:â€:
The macro below should delete any number of paragraphs with any content
between table 10 and the first paragraph that includes the text “Table 11:â€.
I have applied PageBreakBefore to that paragraph. See the comments in the
code.


Sub DeleteParasAfterTableUntilSpecificText()

Dim oRange As Range

Set oRange = ActiveDocument.Tables(1).Range

With oRange
'Collapse range at end of table (in the first paragraph below the
table)
.Start = .End
'Add one paragraph at a time until "Table 11:" is in the next
paragraph
Do While InStr(1, oRange.Paragraphs.Last.Next, "Table 11:") = 0
.End = .Paragraphs.Last.Next.Range.End
Loop
'Now the range contains the correct data - delete
.Delete
End With
'=========================
'oRange is now start of the paragraph with "Table 11:"
'Apply PageBreakBefore to that paragraph
oRange.ParagraphFormat.PageBreakBefore = True
'=========================

'Go to start of document - leave out if you want to keep the original
selection
Selection.HomeKey (wdStory)

'Clean up
Set oRange = Nothing
End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
C

cjg.groups

Last question first: to return to the top of the document, you can use:

Selection.HomeKey (wdStory)

I can see that your code inserts a page break. If the purpose is to make
“Table 11:” start on a new page, it is much better to apply PageBreakBefore
to that paragraph. Manual page breaks may, for example, result in empty pages
if you change the content before the break.

Deletion of text between tables 10 and “Table 11:”:
The macro below should delete any number of paragraphs with any content
between table 10 and the first paragraph that includes the text “Table 11:”.
I have applied PageBreakBefore to that paragraph. See the comments in the
code.

Sub DeleteParasAfterTableUntilSpecificText()

    Dim oRange As Range

    Set oRange = ActiveDocument.Tables(1).Range

    With oRange
        'Collapse range at end of table (in the first paragraph below the
table)
        .Start = .End
        'Add one paragraph at a time until "Table 11:" is in the next
paragraph
        Do While InStr(1, oRange.Paragraphs.Last.Next, "Table 11:") = 0
            .End = .Paragraphs.Last.Next.Range.End
        Loop
        'Now the range contains the correct data - delete
        .Delete
    End With
    '=========================
    'oRange is now start of the paragraph with "Table 11:"
    'Apply PageBreakBefore to that paragraph
    oRange.ParagraphFormat.PageBreakBefore = True
    '=========================

    'Go to start of document - leave out if you want to keep the original
selection
    Selection.HomeKey (wdStory)

    'Clean up
    Set oRange = Nothing
End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word

That's amazing, it worked very well. This helps me see how Ranges can
be used, and how Paragraphs contain text. Thank you very much!
 
L

Lene Fredborg

You are welcome. I am glad I could help.

--
Best regards
Lene Fredborg
Microsoft MVP (Word)
DocTools - Denmark
http://www.thedoctools.com


<[email protected]> skrev i en meddelelse
Last question first: to return to the top of the document, you can use:

Selection.HomeKey (wdStory)

I can see that your code inserts a page break. If the purpose is to make
“Table 11:” start on a new page, it is much better to apply
PageBreakBefore
to that paragraph. Manual page breaks may, for example, result in empty
pages
if you change the content before the break.

Deletion of text between tables 10 and “Table 11:”:
The macro below should delete any number of paragraphs with any content
between table 10 and the first paragraph that includes the text “Table
11:”.
I have applied PageBreakBefore to that paragraph. See the comments in the
code.

Sub DeleteParasAfterTableUntilSpecificText()

Dim oRange As Range

Set oRange = ActiveDocument.Tables(1).Range

With oRange
'Collapse range at end of table (in the first paragraph below the
table)
.Start = .End
'Add one paragraph at a time until "Table 11:" is in the next
paragraph
Do While InStr(1, oRange.Paragraphs.Last.Next, "Table 11:") = 0
.End = .Paragraphs.Last.Next.Range.End
Loop
'Now the range contains the correct data - delete
.Delete
End With
'=========================
'oRange is now start of the paragraph with "Table 11:"
'Apply PageBreakBefore to that paragraph
oRange.ParagraphFormat.PageBreakBefore = True
'=========================

'Go to start of document - leave out if you want to keep the original
selection
Selection.HomeKey (wdStory)

'Clean up
Set oRange = Nothing
End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word

That's amazing, it worked very well. This helps me see how Ranges can
be used, and how Paragraphs contain text. Thank you very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top