macro to repeat commands until end of document

K

ka2cil

Would like to reformat word document by creating a macro to first assign the
margins, then repeat a set of find/replace & formatting commands until the
end of the document. What is the syntax or command to indicate the end of
document (active document)?
 
B

Bear

ka2cil:

You don't need to know where you are in a document to do a global search and
replace. Please use the VBA Help files to learn about the following sample
code:

~~~~~
Dim objDocumentRange As Range

Set objDocumentRange = ActiveDocument.Range

With objDocumentRange.Find
.Style = strTargetStyle
.Replacement.Style = strResultStyle
.Execute Replace:=wdReplaceAll
End With
~~~~~

The line .Execute Replace:=wdReplaceAll ensures that the replacement happens
globally.

Bear
 
K

ka2cil

Bear said:
ka2cil:

You don't need to know where you are in a document to do a global search and
replace. Please use the VBA Help files to learn about the following sample
code:

~~~~~
Dim objDocumentRange As Range

Set objDocumentRange = ActiveDocument.Range

With objDocumentRange.Find
.Style = strTargetStyle
.Replacement.Style = strResultStyle
.Execute Replace:=wdReplaceAll
End With
~~~~~

The line .Execute Replace:=wdReplaceAll ensures that the replacement happens
globally.

Bear

Thanks Bear for your response. I'm new to creating macro and find the vba
help a bit confusing for the quick response that I need, therefore, pardon my
ignorance...

I need to find a certain text in the document, then insert a page break, and
continue to repeat the find & insert page break until the end of the
document. I just need the syntax to indicate the end of document so that I
can use it as the condition for the Do While loop. Thx!
 
B

Bear

ka2cil:

As I understand your application, you want to search for certain text, and
replace that text with a page break plus that text. You can do that with the
regular Find and Replace operation, right?

Find: My special text
Replace with: ^mMy special text

You don't have to know where the end of the document is to to do that
replace globally.

To code the equivalent operation in VBA, you could say something like...

~~~~~
Dim objDocumentRange As Range

Set objDocumentRange = ActiveDocument.Range

With objDocumentRange.Find
.Text = "My special text"
.Replacement.Text = "^mMy special text"
.Execute Replace:=wdReplaceAll
End With

set objDocumentRange = Nothing
~~~~~

This inserts the page breaks everywhere in the document.

Bear
 
K

ka2cil

Thx Bear, this is great! Except, I don't need it for the first occurrance,
which, usually is the beginning of the document. Hence, my document now
start with an empty page.

I tried adding the following after your stmts, thinking that would get rid
of the leading empty page, but it didn't. Any advice in bypassing the first
occurance of the find/replace?

With objDocumentRange.Find
..Text = "^m^m"
..Replacement.Text = "^m"
..Execute Replace:=wdReplaceAll
End With
 
B

Bear

ka2cil:

If you let me write all your code, no matter how pressing your business
needs, you're doing yourself a disservice. I 'm going on with the provision
that you promise to find out the difference between a Range and a Selection.

IF the key text always begins within the first ten characters in the
document, you can avoid looking there with your Find. You'd add a line to
move the starting point of the range (that part of the document you're
searching) ahead ten (or whatever)characters.

Here's the original line, and the line you'd need to add.

~~~~~
Set objDocumentRange = ActiveDocument.Range
objDocumentRange.Start = 10
~~~~~

IF you can't guarantee that the text falls at the beginning, but you know
you ALWAYS want to discard the first hit, you'll have to do two finds. One to
locate the first instance so you can move the start of objDocumentRange, and
another to globally replace (within what's left of objDocumentRange).

Try this:

~~~~~
Sub Paginator()

Dim objDocumentRange As Range

' Set the range (search area) to the entire
' document
Set objDocumentRange = ActiveDocument.Range

' Find the first instance, this resets the
' range to the text found. Collapse the range
' then re-extend the end to the document end.
With objDocumentRange.Find
.Text = "Heading"
.Execute
' Is the text found?
If .Found = True Then
' Collapse then expand the range
' to search from the first instance
' to the end
objDocumentRange.Collapse Direction:=wdCollapseEnd
objDocumentRange.End = ActiveDocument.Range.End
Else
' If there's no text, display a message and exit
MsgBox Prompt:="No special text was found.", _
Buttons:=vbExclamation, _
Title:="Paginator"
Set objDocumentRange = Nothing
Exit Sub
End If
End With

' Replace everything in the new range (which
' now excludes the first instance
With objDocumentRange.Find
.Text = "Heading"
.Replacement.Text = "^mHeading"
.Execute Replace:=wdReplaceAll
End With

Set objDocumentRange = Nothing

End Sub
~~~~~

Bear
 
K

ka2cil

Bear, thank you very much for your comprehensive codes. Due to time
constrain, I'm going to do the simple work around by deleting the empty page
in the beginning of the document. However, I promise you that I'll work on
refining my codes later on and try to understand the language a bit better.

Thanks much for all your help, it's greatly appreciated!! Have a great day!
 
C

CS Hayes

Bear,

The Paginator, Bear's MS version of the Terminator

"I'll be Bach, you be Beethoven!"

"Write your own code or I will Paginate YOU."

LOL
 

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